/    Sign up×
Community /Pin to ProfileBookmark

Hello all,

im abit stuck, i have an iframe and i want to run a certain script dependant to the domain (‘http://www.###.com‘ part) the iframe is on. Im using the iframe as a browser.

For example, i want to run a script if the iframes domain is on google, currently in using:

[CODE]
if1=document.getElementById(‘my_iframe’).src;
as1=if1.match(‘google.com/’);
if (as1==”google.com/”) {
alert(‘good’);
}
else {
alert(‘bad’);
}
[/CODE]

the problem with that is that it isnt the best way to get the domain and you cant have multiple domains (eg if google or yahoo).

So does anyone know of a way to get around this thanks ?
aNdreW

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@ndtrevivJun 16.2006 — How about:
[CODE]
var currHref;

if (document.all) {
currHref = document.frames('my_iframe').location;
} else {
currHref=document.getElementById('my_iframe').contentWindow.location;
}

var searchEngines = {"google.com/","yahoo,com/","excite.com/"};
var isSearchEngine = false;
for(var i = 0; i < searchEngines.length; i++){
if( (href.indexOf(searchEngines)) != -1) isSearchEngine = true;
}

if(isSearchEngine){
do stuff
} else {
do other stuff
}

[/CODE]
Copy linkTweet thisAlerts:
@robo_neojetsamauthorJun 17.2006 — hmm, it dosent seem to be working...

i tried this and it didnt work:
[CODE]
<script>
function testIframe() {
var currHref;

if (document.all) {
currHref = document.frames('my_iframe').location;
} else {
currHref=document.getElementById('my_iframe').contentWindow.location;
}

var searchEngines = {"google.com/","yahoo.com/","excite.com/"};
var isSearchEngine = false;
for(var i = 0; i < searchEngines.length; i++){
if( (href.indexOf(searchEngines)) != -1) isSearchEngine = true;
}

if(isSearchEngine){
alert('Ok');
} else {
alert('Nope');
}
}
</script>
<iframe id="my_iframe" src="http://www.msn.com" onload="testIframe()"></iframe>
[/CODE]


Please help ?
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — For example, i want to run a script if the iframes domain is on google...[/QUOTE]
You're not going to find it too possible to accomplish that goal. The reason is because it is against security protocols for script to access anything having to do with a domain that is not the same as the domain in which the script is executing. You'll see this if you check your error messages after trying to access the Google domain name.
Copy linkTweet thisAlerts:
@robo_neojetsamauthorJun 17.2006 — i have heard about that type of error, but the url is widely avaliable and it is a matter of extraction the domain out of that url and compairing it to ones which will be 'allowed'. The part im not sure about, is checking multiple domains, and if its possible, a more accurate way to obtain the domain from the url.
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — ... a more accurate way to obtain the domain from the url.[/QUOTE]
The following is the most correct method:

var url = self.frames["my_iframe"].location.hostname.toLowerCase();

if (0 <= url.indexOf("google.com") {

alert("It's Google!");

}

else {

alert("Error!");

}
Copy linkTweet thisAlerts:
@robo_neojetsamauthorJun 17.2006 — i managed to get that working, but is it possible to support multiple domains?
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2006 — Put them in an array and loop until you get a match or until the end of the array -- whichever comes first.
Copy linkTweet thisAlerts:
@robo_neojetsamauthorJun 18.2006 — arggg!

yet another problem, for some reason this code keeps on coming up as 'good', i dont know why though ?

[CODE]
<script>
function checkG() {
var Url = document.getElementById('iframe').src.toLowerCase();
var allowed = new Array('google.com','yahoo.com');
for (var i=0; i < allowed.length; i++) {
if ( Url.indexOf(allowed) ) {
var found = true;
}
}
if (found) {
alert('good');
}
else {
alert('bad');
}
}
</script>
<body onclick="checkG()">
<iframe src="http://www.msn.com" id="iframe"></iframe>
[/CODE]
Copy linkTweet thisAlerts:
@phpnoviceJun 18.2006 — This:

if ( Url.indexOf(allowed) )

should be this:

if ( 0 <= Url.indexOf(allowed[i]) )
×

Success!

Help @robo_neojetsam 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,
)...