Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass variables from one iframe to another iframe (in same domain)using javascript only ?

Status
Not open for further replies.

Puma12

Programmer
Feb 14, 2013
3
NL
Could any one show me how i can pass variables from one iframe to another iframe ? suppose we got 1iframe, 2iframe and 3iframe in main page. How i can pass variable1 and variable2 from 2iframe to 3iframe so i can use it in 3iframe ? Hope i get some help with this.Thanks

(Note:i am looking for pure javascript solution )


2iframe javascript:

JavaScript:
<a href="javascript: sendTo3Iframe('56876786565345','tryit')">send</a>

    function sendTo3Iframe(variable1, variable2) 
  {
    //how to pass variable1 and variable2 to 3iframe?
 }

 
Hi

The most simple way is probably this :
Code:
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"iframe1"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"inner1.htm"[/i][/green][b]></iframe>[/b]
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"iframe2"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"inner2.htm"[/i][/green][b]></iframe>[/b]
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"iframe3"[/i][/green] [maroon]src[/maroon][teal]=[/teal][green][i]"inner3.htm"[/i][/green][b]></iframe>[/b]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"parent.iframe3.from1=11"[/i][/green][b]>[/b]send[b]</a>[/b]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"parent.iframe3.from2=22"[/i][/green][b]>[/b]send[b]</a>[/b]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"alert(from1+from2)"[/i][/green][b]>[/b]show[b]</a>[/b]
Just click them in order : send, send, show. No error checking.

Feherke.
[link feherke.github.com/][/url]
 
I'm a bit curious about this: which is the objective for this variable movements? If you're in the same domain and you have control over the source of all iframes maybe there's another solution

Cheers,
Dian
 
Thanks alot . it worked very good. Is it possible to call a function inside iframe3 after clicking? :

<a href="#" onclick="parent.iframe3.from2=22">send</a>

so it automatically show the passed variable and probably do some action on it(without me having to clicking show button) ?in another word calling a function inside iframe3 from iframe2?


 
Hi

With this code there is no more need for separate inner2.htm, inner1.htm is used twice :
Code:
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]iframe1 src[/i][/green]=[maroon]inner1[/maroon].[maroon]htm[/maroon][b]></iframe>[/b]
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]iframe2 src[/i][/green]=[maroon]inner1[/maroon].[maroon]htm[/maroon][b]></iframe>[/b]
[b]<iframe[/b] [maroon]name[/maroon][teal]=[/teal][green][i]iframe3 src[/i][/green]=[maroon]inner3[/maroon].[maroon]htm[/maroon][b]></iframe>[/b]
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"parent.iframe3.setandshow(window.name,11)"[/i][/green][b]>[/b]send 11[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"parent.iframe3.setandshow(window.name,12)"[/i][/green][b]>[/b]send 12[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"parent.iframe3.setandshow(window.name,13)"[/i][/green][b]>[/b]send 13[b]</a>[/b]
Code:
[b]var[/b] store[teal]=[/teal][teal]{}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]setandshow[/color][teal]([/teal]sender[teal],[/teal]value[teal])[/teal]
[teal]{[/teal]
  store[teal][[/teal]sender[teal]]=[/teal]value

  [b]var[/b] count[teal]=[/teal][purple]0[/purple][teal],[/teal]total[teal]=[/teal][purple]0[/purple]
  [b]for[/b] [teal]([/teal][b]var[/b] i [b]in[/b] store[teal])[/teal]
    [b]if[/b] [teal]([/teal]store[teal].[/teal][COLOR=darkgoldenrod]hasOwnProperty[/color][teal]([/teal]i[teal]))[/teal] [teal]{[/teal]
      count[teal]++[/teal]
      total[teal]+=[/teal]store[teal][[/teal]i[teal]][/teal]
    [teal]}[/teal]

  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]
    [green][i]'Just received '[/i][/green][teal]+[/teal]value[teal]+[/teal][green][i]' from '[/i][/green][teal]+[/teal]sender[teal]+[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green][teal]+[/teal]
    [green][i]'The total of the values received until now from '[/i][/green][teal]+[/teal]count[teal]+[/teal][green][i]' frames is '[/i][/green][teal]+[/teal]total
  [teal])[/teal]
[teal]}[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top