Forum Moderators: open

Message Too Old, No Replies

Iframe URL

How do I find URL

         

Pantyboy

11:06 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Hi all

I want to be able to disable a link depending on the URL of the page in an <iframe>.

I can do this if the URL is in the address bar using 'InString=top.document.URL' but I have tried every whichway to get it to work with my <iframe>.

My code looks like this.


function disLink(FName, LText, MText) {

InString = top.document.URL

LastSlash=InString.lastIndexOf ('\\', InString.length-1)
OutString=InString.substring (LastSlash+1, InString.length)

if (OutString == FName) {
document.write ("<font class='dllink'><b>"+ LText +"</b></font>")
}
else {
document.write ("<a href=" + FName + " target='_top' onclick='blur(); window.status=\"" + MText + "\"; return true' class='dlink' onmouseover='window.status=\"" + MText + "\"; return true'><b>" + LText + "</b></a>")
}
}

So, what do I need to change to get it to work with my <iframe>'s URL and to what do I need to change it to?

Many thanks in advance.

Pantyboy

ajkimoto

3:55 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



Pantyboy,

Why not just go after the src of the iframe?

<body>
<iframe id="myframe" src="http://www.google.com"></iframe>
<button onclick="alert(document.getElementById('myframe').src)">get url</button>
</body>

ajkimoto

Pantyboy

10:48 pm on Jul 10, 2004 (gmt 0)

10+ Year Member



Hi ajkimoto

Many thanks for the tip. I've tried your script and it may well do what I want.

I haven't had the chance to try it 'live' so to speak, but I'll let you know in the next fews days if it's a success.

Many thanks

Pantyboy

Pantyboy

1:57 pm on Jul 11, 2004 (gmt 0)

10+ Year Member



Hi ajkimoto

Tried your idea in my code and it doesn't seem to work.

I need a replacement for the line:


InString = top.document.URL

to pick up the url of the <iframe> and no matter how I alter your example to fit here, I get either errors or nothing. How would you alter this line?

Regards

Pantyboy

Rambo Tribble

2:08 pm on Jul 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bear in mind, if the URL for the document in the iframe is from a different domain than the parent document, the Same Origin Policy will make that address unavailable to scripts in the parent.

I think this is why ajkimoto suggested using the src value, but this assumes the iframe was loaded by the parent.

Pantyboy

10:58 pm on Jul 11, 2004 (gmt 0)

10+ Year Member



Hello there Rambo Tribble

When I use <button> example, the getElementById works and works well. The problem is, it doesn't work in this instance.

The first document is loaded with the page. A button is then clicked and the document in the <iframe> is changed. So that I can change the look of the button depending on which document is loaded into the <iframe>, I need to to be able to get the URL of the <iframe> document and that's the problem.

The documents loaded into the <iframe> are in the same domain just different directories.

Regards

Pantyboy

Rambo Tribble

2:26 am on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, if you have an iframe named fraOne (name="fraOne", not id), the syntax would be:

top.fraOne.document.URL

If you used the same value (fraOne) for an id, the syntax would be:

top.document.getElementById('fraOne').document.URL

You see, there are two document objects in the scope chain of the latter. (By the way, I don't mean to imply you should have an element with both a name and id attribute that have the same value. You can have both attributes, but conflicts can develop if they have the same value.)

Pantyboy

3:31 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Hi again all

I've found where I was going wrong and it's was so obvious, I need a good kicking.

First, Rambo Tribble. The expression 'top...' is the right one for my use. Once I figured out what the problem was, it worked like a dream.

The problem was - and I think you'll agree I should be taken to one side and given a real good slap - I was loading the <iframe> after the script, so as far as the script was concerned the <iframe> didn't exist. When I loaded the <iframe> first and the script second, it worked.

Once again, many thanks

Pantyboy (right royally slapped)

Rambo Tribble

4:24 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh my! There'll be no pudding for you.