Javascript and iframe newb infuriating problem

Status
Not open for further replies.
I'm sure it's a simple fix.

I am taking an intro to JavaScript class online - the professor is completely disengaged and doesn't respond to email, Blackboard notes, anything. FML and FMT (tuition).

Anyway, I have a page that looks like this:

Code:
<div id="wrapper">

     <div id="navigation"> 
          
          links 
     
     </div>

     <div id="content">

          links to other pages that go in the if ram below

          [iframe here]

     </div>

</div>

After I click on a link in the content div to bring up a page in the iframe, if I try to click on a link back in the navigation div, it opens in the iframe.

I'm sure I'm supposed to somehow use "target" but no matter where I put target and no matter if I put _self or _top or _parent the links outside the iframe keep opening inside the iframe after I click on a link that is supposed to go in the iframe.

Here's the Javascript

Code:
window.onload = initLinks;

function initLinks() {
	for (var i=0; i<document.links.length; i++) {
		document.links[i].target = "icontent";
	}
}

Solving this very simple problem will grant you instant access into Heaven.

Thanks!
 

minnmass

Ars Tribunus Militum
2,087
Subscriptor
I'm not a JavaScript expert, but it looks like you're using JS to set the target to all of your links to open in the icontent context (if that's the right word) - which I assume is your iFrame.

... all of your links, when the page loads, which would probably overwrite any other targeting those links would already have. You probably need some kind of conditional logic in your for loop (maybe, only set the target if the link doesn't already have one? ... or filter out links that are in the navigation div?).
 
I'm going to say we don't have enough context to help you solve your homework problem.

1. We need to know more about the problem definition and exactly how the result is supposed to behave. The question may or may not in fact just suck (it's possible).
2. We need the full code listing of what you have so far. This will let us point out exactly what your code is actually doing vs what you want it to be doing.

Help us help you! :p

If you don't want to do all that, I'd say minnmass has the right place to start, but really there isn't enough info to say what the exact issue is.
 

gnosis

Ars Tribunus Angusticlavius
8,339
You don't need JavaScript for this at all. Set the `target` attribute for the links you want to point to `icontent`, and don't set the `target` attribute for links you want to load in the current window context.

Edit for example:

Code:
<div id="wrapper">

     <div id="navigation"> 
          
          <a href="/example">This opens in the current window context</a> 
     
     </div>

     <div id="content">

          <a href="/whatever" target="icontent">This opens in the iframe</a>

          <iframe name="icontent"></iframe>

     </div>

</div>
 

glenthas

Ars Tribunus Angusticlavius
6,979
Subscriptor++
[url=http:/civis/viewtopic.php?p=26378031#p26378031:3d3e4s0r said:
gnosis[/url]":3d3e4s0r]You don't need JavaScript for this at all. Set the `target` attribute for the links you want to point to `icontent`, and don't set the `target` attribute for links you want to load in the current window context.

Edit for example:

Code:
<div id="wrapper">

     <div id="navigation"> 
          
          <a href="/example">This opens in the current window context</a> 
     
     </div>

     <div id="content">

          <a href="/whatever" target="icontent">This opens in the iframe</a>

          <iframe name="icontent"></iframe>

     </div>

</div>

Since it's a JavaScript class, I suspect this misses the point.
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=26375807#p26375807:2jpm33qm said:
minnmass[/url]":2jpm33qm]I'm not a JavaScript expert, but it looks like you're using JS to set the target to all of your links to open in the icontent context
Yup, looks exactly like what he's doing.

[url=http:/civis/viewtopic.php?p=26487803#p26487803:2jpm33qm said:
topham[/url]":2jpm33qm]
[url=http:/civis/viewtopic.php?p=26486169#p26486169:2jpm33qm said:
btmlltt[/url]":2jpm33qm]It's not 1999 so don't use an iframe at all. Also, quit this 'school' that you're taking this 'class' from.

iframes sill have their uses, but yeah, they are a general solution to a problem.
I'd agree and in this case it seems like it's probably a reasonable way to demonstrate js property/attribute manipulation. Immediate and obvious changes that result from what you're doing in the js.
 
Status
Not open for further replies.