Results 1 to 6 of 6

Thread: Run Javascript Line From Inside A Frame

  1. #1
    Join Date
    Dec 2004
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Run Javascript Line From Inside A Frame

    Hi,

    I really need some help, I created an index with a frameset of two columns, one is the menu and the other is the main content.I have a link in the menu to execute a javascript code that highlights all the members of a friendslist in the 'main' frame, anyway when I run the script directly in the browsers address bar it works fine, but when I click on the link inside the menu frame it doesn't work, i've been scratching my head for hours and can't seem to come up with anything, this is the link I'm using:

    Code:
    <a href="javascript:elms=document.getElementById('friends').getElementsByTagName('li');for(var fid in elms){if(typeof elms[fid] === 'object'){fs.click(elms[fid]);}}" target="main">Click Here</a>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd forget about using the address bar in cases like this (where frames are involved) for testing code. Since it is unclear to me where the actual script is, let's go over the basics.

    In javascript when you are in one frame of a basic frameset, the frameset page is the parent, and the individual frames are the parent's frames collection.

    Now if you want to do anything at all complex in either frame, it is best to have the script for that on the page in the frame where you want these things to happen. That way anything defined within that script can easily point to that frame's document. In this case, your code for highlighting stuff in main should be on the page in main.

    So, let's say you are in the first frame - the menu (parent.frames[0]), and you want to execute a function called myFunc in the second frame - the main page (parent.frames[1]):

    Code:
    <a href="#" onclick="parent.frames[1].myFunc(); return false;">Whatever</a>
    That should do it for you, just have the myFunc function on the main page do the work of the desired highlighting there.

    Assuming that there is an fs and fs.click() on main, this might be good code for myFunc on main:

    Code:
    function myFunc(){
    	var elms=document.getElementById('friends').getElementsByTagName('li');
    	for(var fid in elms){
    		if(typeof elms[fid] === 'object'){
    			fs.click(elms[fid]);
    		}
    	}
    }
    Last edited by jscheuer1; 11-10-2009 at 05:12 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2004
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey,

    Thanks for replying .

    I think I understand what your saying but the problem is the the page that loads in the second frame isn't on my web server and I have no control over the codes in it :S, this is what I'm doing:

    index.html:

    HTML Code:
    <html>
    
    <HEAD>
    <TITLE>» </TITLE>
    
    </HEAD>
    
    <frameset cols="20%,80%" border="0" scrolling="no" >
      <frame src="menu.html" name="menu" />
      <frame src="http://www.URLofThirdPartyForum.com/" name="main" />
    </frameset>
    
    </html>
    Menu.html:

    HTML Code:
    <HEAD>
    <TITLE>» </TITLE>
    
    <style type="text/css">
    body
    {
    background-color:#3b5998;
    }
    </style>
    
    
    </HEAD>
    
    <img src="fbpt.png"> 
    
    <br />On every new page of friends, <u><a href="javascript:elms=document.getElementById('friends').getElementsByTagName('li');for(var fid in elms){if(typeof elms[fid] === 'object'){fs.click(elms[fid]);}} " target="main"><font color="#ffffff">Click Here</font></a></u> to automatically highlight all your friends on that page. 
    
    © <script type="text/javascript">
    var theDate=new Date()
    document.write(theDate.getFullYear())
    </script>. All rights reserved.</font>
    Last edited by maverick; 11-10-2009 at 11:37 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's your problem then. There is no cross domain javascript communication between pages without the consent of those in control of each of the domains and the use of third party certs.

    If you don't have the consent, and/or are not willing to obtain the certs, it just won't work like that.

    This is more or less the minimum protection the web affords against having content from one site displayed as though it were content on another site. If you want to do that (legally or illegally), in most cases you at least have to make your own copy.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2004
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But I don't want to steal any content from any website, I want to provide an additional service (the ability to invite their friends to the forum without having to click on each members username one by one) to certain users to an already well established forum.

    All I wanted to know is how come, when I'm in the members page and I copy and paste this code in the address bar and then hit enter:

    HTML Code:
    javascript:elms=document.getElementById('friends').getElementsByTagName('li');for(var fid in elms){if(typeof elms[fid] === 'object'){fs.click(elms[fid]);}}
    It automatically highlights all the members on that page, but when I try to do it through a frame using a <a link method, it doesnt work.Surely there has to be a way to make that same script that works in the address bar work through a link inside a frame?

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think I already answered that question. I never actually said you were trying to do anything wrong. I don't even think I have enough information to decide that issue. It's just that the built in security features of browsers as regards cross domain scripting that I mentioned won't allow what you want to do. If they did, they would also allow all sorts of questionable and worse practices. That's all.

    If you thought I was accusing you of anything, I didn't mean to be. Please accept my apologies as regards that part of it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •