Forum Moderators: open

Message Too Old, No Replies

How to get the address of the current page (in browser) with Javacript

         

defireman

8:29 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



I want to create a popup that will collect the url of the current page showing in a user's browser and put it in a text box, so that it can be sent to a website. Is there a way to use Javascript to obtain the URL of the current site in the browser window?

Thanks for your help.

Bernard Marx

9:49 pm on Sep 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



alert(window.location.href);

jeff5311

4:12 am on Sep 29, 2005 (gmt 0)

10+ Year Member



i have a similar problem...

i'd like to log the urls of sites that are serving my banner ad. could this be done via javascript that calls a php/mysql back-end? can someone suggest a good way to do this?

would something like top.href.location work for me?

thanks!

jeff5311

4:13 am on Sep 29, 2005 (gmt 0)

10+ Year Member



oops! i meant 'top.location.href'

Bernard Marx

8:09 am on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your banner is an image then this is purely a server-side issue. In most cases, there should be a referrer header in the HTTP request that you can log. Some people disable this header in their browser.

jeff5311

2:35 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



thanks for your response!

the banner we're employing is a php file within an <IFRAME>. we can use php to get the header, but it only succeeds ~50% of the time (server side).

that's why i was wondering if there was a way to do this in javascript and pass the referer in to the php file so it can be logged.

does this make sense?

Bernard Marx

5:42 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. In that case, you can get the location via: parent.location.href

1) parent may be better than top - perhaps the calling page is itself in a frame.
2) There are security restrictions concerning cross-domain scripting, but I'm fairly sure that you can still read the location of the parent frame, even if it is in another domain - check this though.

Trouble is, you then need to find a way of sending this information back to your server
(all of these will require JS to be enabled, of course).

1) Load an image with the src as a script page, with a search string.

This method may be subject to privacy measures. There's no doubt a way that you can send the information in the requested filename itself (out of my league in server-scripting terms).

2) XMLHTTPRequest

Supported in more recent browsers.

3) Invisible Iframe

Probably the simplest, and least likely to cause complications - especially considering the simplicity of the task.

Include an invisible IFRAME in your own IFRAME. Then (after load) set the iframe's src to your serverscript page, sending the info in the request string.

jeff5311

6:32 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



Bernard... you're awesome!

with your help i think i've discovered a solution. i've tried:

1) getting the parent url via javascript (url of the site serving the ad)

2) pass it as a variable to my php banner ad (like banner.php?url=http://www.example.com)

3) then, log the url in my banner.php file

however, the url using parent.location.href isn't consistently being passed in. i guess this could be due to lack of javascript support on all browsers?

Bernard Marx

8:45 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It could be. Not sure what JS is support levels are 90% ish (?) - depending a lot on the "type" of audience for each site.

Having said that, if these inconsistencies are entirely due to client-side JS support, I would expect all banner-carrying sites to "report in" to some extent. That would make sense, wouldn't it? Do your results support that?

jeff5311

3:17 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



my reports are showing much fewer reporting browsers/referrers... about 50%.

So, I must be doing something wrong... some of the ad networks that we're employing wrap our <IFRAME> banner within their own <IFRAME> to serve our ad.

However, top.location.href should always find the highest level URL, right?

Bernard Marx

4:44 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes.