/    Sign up×
Community /Pin to ProfileBookmark

Use of "function open_win" on frame pages

I have created a web page with three frames. One frame contains several links, and each link is supposed to open two pages, one in each of the other two frames. Here is the code I am using:

<html>
<head>
<script type=”text/javascript”>
function open_win(one)
{
window.open (“http://www.cbs.com/”,”main“)
window.open (“http://www.cnn.com/”,”character“)
}
function open_win(two)
{
window.open (“http://www.msnbc.com/”,”main“)
window.open (“http://www.abc.com/”,”character“)
}
</script>
</head>

<body>
<a href=”javascript:open_win(‘one’)”>One</a><br>
<a href=”javascript:open_win(‘two’)”>Two</a>
</body>
</html>

With this code, both links open the second set of links (function open_win(two). I’ve tried countless variations of this code, but haven’t been able to hit the right combination. Any thoughts about what I’m doing wrong, or not doing right?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledOct 11.2008 — first mistake you should understand, very important. whenever you give two or more thing the same indentifier name, the last readed statement overwrite the previous; that apply for everything: object, function, variable. that is:
<i>
</i>var [COLOR="YellowGreen"]num[/COLOR] = 0;
function [color='yellowgreen']num[/color](){}
alert(num);

notice the identifier ([color='yellowgreen']green color text[/color]) are the same. since they are the same, function [B]num[/B] overwrite the variable [B]num[/b], hence the value of variable [B]num[/B] is lost. in your case, the second function overwrite the first. there is various solution, some aren't good practice and some are better to mantain. you can give different identifier to each function or you can have one function for all of them and simply pass as parameter the url you want to open.

second, according to your topic you're trying to load documents on frames, not on new windows. [B]window.open[/B] is solely used to open new windows, not to change the frame's url. you have to refer to each frames so you can change their source and that might be according on how they are nested. i will assume you have a simple nesting. a document that hold three frames: a header, a menu and the main content.
[code=html]
<frameset rows='200, *'><frame src='header_path' name='header' />
<frameset cols='200, *'>
<frame src='menu_path' name='menu' />
<frame src='content_path' name='content' />
</frameset>
</frameset>
[/code]


assuming that link from the menu frame will update document from header and content frames, javascript code is:
<i>
</i>function frameUpdate(parentSource, mainSource){
parent.header.src = parentSource;
parent.content.src = mainSource;
}

[code=html]
<a href='document.htm' target='content' onclick='frameUpdate("/header_source.htm", this.href); return false;'>link</a>
<a href='document.htm' target='content' onclick='frameUpdate("/header_source.htm", "/other_document.htm"); return false;'>link</a>
[/code]


this sample show two different way you can use the function [B]frameUpdate[/B], you can pass a literal string or the string from the current link href attribute.
Copy linkTweet thisAlerts:
@Lon33authorOct 12.2008 — Thanks ZeroKilled.

The consequences of "ovewrite[s]" that you explained were exactly the symptoms I was suffering.



And knowing that I should have been using the "frame Update" function, instead of the "window open" function, helped a lot.
×

Success!

Help @Lon33 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...