• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1263
  • Last Modified:

parent.document.getElementById from iFrame to parent not working

An "onClick" call to a Javascript function to set the href of a button on a toolbar does not find the button when the call is made from an iFrame. The toolbar is in the main window.

alink = parent.document.getElementById('members_link');
or
alink = window.parent.document.getElementById('members_link');

This works when the link clicked is in the main window; it only fails when the link is in the iFrame.

I'm using Chrome, but also tested IE8.

Here's the menubar button:

<a id=members_link
      onClick=\"myLink='M'\"
      onmouseover=\"roll_over('members_button', 'images/menubar_Members_hi.jpg');\"
      onmouseout= \"roll_over('members_button', 'images/menubar_Members.jpg');\"
      href=\"directory.php\" target=main
      ><img class=menu_image src='images/menubar_Members.jpg' NAME=members_button></a>

The page only has this one iFrame.
0
wilri01
Asked:
wilri01
  • 4
  • 3
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
try this from inside the iframe:
var alink = parent.$(parent.document).getElementById('members_link');

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
that might also work:
var alink = parent.$("members_link");

Open in new window

0
 
wilri01Author Commented:
sedgwick,
What do I need to do to use JQuery?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Meir RivkinFull stack Software EngineerCommented:
add this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Open in new window

0
 
wilri01Author Commented:
Sorry, but neither form works.
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
The parent document and iframe document must be in the same domain for the child iframe document to access the parent DOM. It is considered a security issue when the documents are in separate domains.
0
 
wilri01Author Commented:
Yes, I was aware of that, but it is the same domain.

BUT... you got me thinking.  I looked at how the link is being constructed for the button, and found it includes "www." and the URL I used to get ot the page left that off.

So I add "www." to the URL, and the Javascript code now works!

So the problem is, how can I tell if the user entered the "www.", or left it off?  Maybe use a relative HREF that doesn't include the domain name?

Maybe some kind of "rewrite" in Apache to add the "www." if it isn't entered?

God, I'm so happy to see this working at all!  THANK YOU SO MUCH!!!
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Can you use a relative url in the button link?
0
 
wilri01Author Commented:
The trick is to NOT include the "www." in the link.  Then the user can use, or not use, the "www." and it works.

I've been working on this off and on for almost a year.  It feels so good to see this work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now