Forum Moderators: open

Message Too Old, No Replies

Opening external Link in a Framed Page

         

Gian04

1:52 pm on Oct 9, 2009 (gmt 0)

10+ Year Member



The Javascript shown by Fotiman in this thread
[webmasterworld.com...]

works perfect for my requirements but the problem is once I clicked back from PageB (framed) to the PageA (The page that called PageB)
All the links of PageA are all javascript link (can be seen if hover the link)

<div id="X">

</div>

<div id="Y">

</div>

How can I make it in such a way that only the links in <div> Y are the only javascript link (will open in a framed page) and all other links of other divs are regular link. Thanks

Fotiman

6:31 pm on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In that thread you referenced, PageA would include this code:

<script type="text/javascript">
window.onload = function () {
var elList = document.getElementsByTagName('a');
var i;
for (i = 0; i < elList.length; i++) {
elList[i].href = "pageB.htm?s=" + encodeURI(elList[i].href);
}
};
</script>

That applies the functionality to all of the elements in elList, which is a collection of all <a> elements on the page. So what you would want to do is reduce the set of <a> elements in the list to be only the ones within <div id="Y"></div>:


<script type="text/javascript">
window.onload = function () {
var Y = document.getElementById('Y');
var elList = Y.getElementsByTagName('a');
var i;
for (i = 0; i < elList.length; i++) {
elList[i].href = "pageB.htm?s=" + encodeURI(elList[i].href);
}
};
</script>

See if that works.

Gian04

1:19 am on Oct 10, 2009 (gmt 0)

10+ Year Member



Thanks it works

except for this (Im getting 403 Forbidden error)

www.example.com/pageB.htm?s=http://www.external_page.com/?r=123456

But this one works fine.

www.example.com/pageB.htm?s=http://www.external_page.com/index.php?r=123456

Is it a problem with my host?

Fotiman

2:29 pm on Oct 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, a 403 error comes from the server. I would contact your host to find out why index.php is not the default when viewing the root path.

Gian04

7:02 am on Oct 12, 2009 (gmt 0)

10+ Year Member




<script type="text/javascript">
window.onload = function () {
var Y = document.getElementById('Y');
var elList = Y.getElementsByTagName('a');
var i;
for (i = 0; i < elList.length; i++) {
elList[i].href = "pageB.htm?s=" + encodeURI(elList[i].href);
}
};
</script>

Is it also possible that if the domain in the <a> tag is MY domain even though it is part of <div id="Y"></div>, it will remain a regular link NOT a javascript link? All other links are javascript.

Fotiman

1:10 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sure, in your for loop just check the elList[i].href value to see if it is in your domain. If so, so nothing. If not, then set elList[i].href as shown above.

Gian04

1:48 pm on Oct 13, 2009 (gmt 0)

10+ Year Member



Sorry Fotiman but I really dont know how to do that in javascript, because the link can be something like

http://www.example.com
http://www.example.com/folder
http://www.example.com/index.php?c=abc123
etc

(where example.com is my domain name)

How can I trap all links that is under my domain. Thanks

Fotiman

3:50 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this:

for (i = 0; i < elList.length; i++) {
if (document.domain !== elList[i].hostname) {
elList[i].href = "pageB.htm?s=" + encodeURI(elList[i].href);
}
}

Fotiman

3:53 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, if you mix links like this:
www.example.com/
example.com/
The above method won't work because document.domain will match only the current domain (either www.example.com or example.com, but not both).