views:

294

answers:

2

Hi! I'm using the following script to force a specific page - when loaded for the first time - into a (third-party) iFrame.

<script type="text/javascript">
    if(window.top==window) {
       location.reload()
    } else {
    }
</script>

(For clarification: This 'embedding' is done automatically by the third-party system but only if the page is refreshed once - for styling and some other reasons I want it there from the beginning.)

Right now, I'm wondering if this script could be enhanced in ways that it's able to detect the current URL of its 'parent' document to trigger a specific action? Let's say the URL of the third-party site is 'http://cgi.site.com/hp/...' and the URL of the iFrame 'http://co.siteeps.com/hp/...'. Is it possible to realize sth. like this with JS:

<script type="text/javascript">
    if(URL is 'http://cgi.site.com/hp/...') {
       location.reload()
    }
    if(URL is 'http://co.siteeps.com/hp/...') {
       location.do-not.reload() resp. location.do-nothing()
    }
</script>

TIA josh

+4  A: 
<script type="text/javascript">
    if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
       location.reload()
    }
    if(/^http:\/\/co.siteeps.com\/hp\//.test(window.location)) {
       location.do-not.reload() resp. location.do-nothing()
    }
</script>

Of course, the second if is redundant so you can simply do this:

<script type="text/javascript">
    if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
       location.reload()
    }
</script>

What you're doing here is testing window.location with a regular expression to see if it matches the URL you want.

If you want to refer to the parent's URL, you can use parent.location.href.

Per your comment, in the event that you want to do something else you can do something like this:

<script type="text/javascript">
    if(/^http:\/\/cgi.site.com\/hp\//.test(window.location)) {
       location.reload()
    }
    else if(/^http:\/\/co.siteeps.com\/hp\//.test(window.location)) {
       //do something else
    }
</script>

If you're doing nothing in the other case, that's effectively a NOP (no operation) so you don't even need the else there (or another if) since it's going to be an empty block.

Vivin Paliath
hi vivin! thx for your instant reply! What should I define instead of 'location.do-not.reload() resp. location.do-nothing()'?
josh
is if...if... possible with js? or do I need an 'else'?
josh
Assuming I understand your logic flow correctly, you *only* want to perform a reload if the URL matches `http://cgi.site.com/hp`. If that's the case, you don't need an else since you really aren't doing anything. Of course, if you want to do something *else* then you can use an `else if(...) { ... }`
Vivin Paliath
nice! will this also work if the actual URL is e.g. 'http://cgi.site.com/hp/test.dll?blabla123.html'?
josh
yes, only if the URL matches resp. starts with http://cgi.site.com/hp
josh
Sure. Currently the regular expression only matches any URL from `cgi.site.com` that is under the `hp` directory. If you want it to match anything from the site, then you have to change the regular expression to `/^http:\/\/cgi.site.com/`. If you want to make it a little more robust (to match https as well for example), you can use `/^https?:\/\/cgi.site.com/`
Vivin Paliath
really amazing... ;) so this is to catch them all, right?<code>if(/^https?:\/\/cgi.site.com/.test(window.location))</code> (or is one '\' missing here after '.com/'?
josh
Yup, that should be good! The `/`'s act as delimiters for regular expressions so anything between the `/` is treated as a regex. As a result, any `/` *inside* the regex needs to be escaped. If you wanted to, you could do `/^https?:\/\/cgi.site.com\//` which means it matches the `/` in the URL after the `.com`. But that shouldn't be necessary. Oh, and if you want highlight anything as code, use backticks `like so`. :)
Vivin Paliath
last question: which one is right? if(/^https?:\/\/cgi.site.com/.test(window.location)) OR if(/^https?:\/\/cgi.site.com/\.test(window.location)) OR if(/^https?:\/\/cgi.site.com/\/.test(window.location))
josh
like so? :) how? I've tried <code>...</code> but that doesn't work.
josh
This is just a `test`.
josh
Ok, now I've got it! :) Vivin, thanks a billion for your support! I'm new to Stack Overflow and you, Sir, just made my day!
josh
The first one is right. The second one is wrong because the regular expression ends at `.com/`. What you're doing with `/.../` is defining a regular expression literal which is a bit like a `RegExp` object. Then, you're calling the `test` method on it. So what you basically have is `regex\.test(...)` which isn't valid Javascript. In the third one you have the same problem. The regex ends at `.com/` and so you essentially have `regex\/.test` which isn't valid Javascript either. If you want to match the trailing `/`, use `/^https?:\/\/cgi.site.com\//`.
Vivin Paliath
No problem Josh :) Welcome to SO! :)
Vivin Paliath
Logical and even not that diffcult to understand if explained that well... once again: Thanks a bunch, Vivin! Bye.
josh
+1  A: 

You could use window.location.href to get the URL to do the string comparison. If you are in an iframe and need to know the parents URL parent.location.href should get you that.

Flash84x