Jump to content

Accessing a parent's information using Javascript

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Hi all, I'm fairly new to web programming, sorry if I'm asking a stupid question here ^^

I'm making a HTML page that uses a seperate HTML page to display a menu in an iframe. The emu itself is written into the second frame using Javascript, so that it can identify the title of the parent frame and use a different background colour for the relevant navigation tab; this is to give the page a shinier look overall. as I am making this website for a business. Here's some of my code, with most of the fluff taken out :)

Font HTML page:

<html lang=en>

<head>

	<title>Home</title>

	<style media="screen" type="text/css">

		@import url(style.css);

	</style>

</head>


<body>

	<IFRAME SRC="navmenu.html" WIDTH=100% HEIGHT=195 ></IFRAME> 


...As you can see, this pretty much just displays the iframe containing the navigation menu right off the bat. In the navmenu...


<head>

<!--Head stuff-->

</head>

<body>

<TABLE WIDTH=100% ALIGN=LEFT HEIGHT=100>

<!--table Stuff-->

<SCRIPT SRC="navMenu.js" TYPE="text/javascript"></SCRIPT>

</Table></Body></etc...>


...This HTML file is just to tweak how the menu displays itself using lots of table stuff. The actual menu is written into this file by way of a javascript file, navmenu.js. I have decided on this kind of modular implementation so people will (hopefully) be able to maintain the website without much knowledge of HTML after I leave. Anyways, here's the all-important .js file:


var loc = parent.document.title;	//I tried this first

var loc = parent.getTitle();		//I tried this next


if (loc == "Home") {

	//display a highlighted tab for this menu choice, showing the user that this is the page they're on}

else {

	//display a regular tab for this menu choice;}


if (loc == "News") {

//display a highlighted tab for this menu choice, showing the user that this is the page they're on}

else {

	//display a regular tab for this menu choice;}


if (loc == "Jobs") {

	//display a highlighted tab for this menu choice, showing the user that this is the page they're on}

else {

	//display a regular tab for this menu choice;}


...So, yeah, first I tried just accessing the parent's title directly, and it came up with a Javascript error;

Unsafe JavaScript attempt to access frame with URL <file> from frame with URL <file> Domains, protocols and ports must match.

...Which I thought was fair enough, after all, we don't want random javascript pop-ups leeching data from main pages. So, then I tried specifically writing a method in the parent (the home page) to return a string containing the title of the document, but got the same error. So now I'm stumped; is there any way to pass data from one HTML file to another in this manner, or am I beating a dead horse?

Please and thank you! ^_^
~Fae
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#2
oldhendra

oldhendra

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hi.
Could the parent pass parameter in URL upon iframe creation:
<iframe src="navmenu.html?title=home"></iframe>

and let your navmenu.html access it,
using javascript's "location.search" ?

Hendra

#3
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
That did work, thanks Hendra ^_^

Like I say, I'm fairly new to web programming, but I'm trying to pick it up quickly. I didn't know whether HTML could pass parameters to an element or not; this'll make things much easier.

Thanks for your help

~Fae
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)