Forum Moderators: coopster

Message Too Old, No Replies

How to recall parent URL

         

tbrim20

1:37 pm on Oct 21, 2010 (gmt 0)

10+ Year Member



I've got plenty of coding experience but I'm pretty much a newbie to php and javascript so bear with me.

Here's the situation...

I have an HTML page and I'm trying to pass a url id (i.e. _http://www.xyz.com/page?id=abc) through to a lightbox.

The lightbox is coded in php and is being called through Javascript (i.e. <script type="text/javascript" src="http://www.xyz.com/js.php"></script>) within the HTML page.

The lightbox runs some javscript and requests some user input. And I want to take that original "url id" from the parent page and insert it in with the user input to tie the two together.

The only thing I have found that I thought would work was to grab the parent url and strip it down to the id using "opener.location.href" but that doesn't appear to be working. I'm assuming that either I'm doing it completely wrong or the original url isn't the parent in this situation?

Any help would be great appreciated!

penders

4:03 pm on Oct 21, 2010 (gmt 0)

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



As far as I'm aware, the traditional lightbox doesn't use a popup window, so you shouldn't have to refer to opener. - simply [window.]location.href should suffice?

Are you also writing your SCRIPT tag with PHP? In which case you could read the id param in PHP (value of $_GET['id']) and append it to the SRC attribute of the SCRIPT element - as you write it to the page?
<script type="text/javascript" src="http://www.xyz.com/js.php?id=abc"></script>


js.php could then read $_GET['id'] in the same way and incorporate it as a hidden field when requesting user input?

tbrim20

4:18 pm on Oct 21, 2010 (gmt 0)

10+ Year Member



Hi Penders and thanks for the reply.

I was thinking the same thing as you but for some reason, window.location.href doesn't work. It just returns nothing.

I've been digging around some more online and all I have found so far is pretty much your suggestion which is to tag the the php file like you suggested.

The page is currently HTML but I can easily change it to php and try it out.

But any other suggestions are welcomed.

Cheers!

penders

6:22 pm on Oct 21, 2010 (gmt 0)

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



window.location.href doesn't work. It just returns nothing.


window.location.href should return something. As you thought, it should be the URL used to access the document in the current window.

What do you get if you simply try:
<script type="text/javascript"> 
alert(window.location.href);
</script>

...at the end of your page?

tbrim20

3:24 am on Oct 22, 2010 (gmt 0)

10+ Year Member



Sorry, I wasn't thinking straight when I said it returned nothing (it did actually return the correct url)...

I had a script set up that was supposed to take the url and grab the "id". And the script was returning nothing (not window.location.href).

But using the alert like you recommended allowed me to troubleshoot the problem and figure out what was going on.

I used the script below (grabbed it online) which works without a problem in HTML:

<script type="text/javascript">
function update_tracking_id(field_id)
{
name = 'id';
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return;
else
document.getElementById(field_id).value = results[1];
}
update_tracking_id('meta_adtracking');
</script>


But when I inserted it into the php file that the lightbox calls which contains the form data, it wasn't working. I'm still not sure why but I'm a just a newbie to javascript.

So I found another way to do it using string subtraction and it worked like a charm:

<script type="text/javascript">
function update_tracking_id(field_id)
{
var url = window.location.href;
var theleft = url.indexOf("=") + 1;
var id = url.substring(theleft);
if( id == null )
return;
else
document.getElementById(field_id).value = id;
}
update_tracking_id('meta_adtracking');
</script>


So I'm happy to report that it's now working.

penders

8:55 am on Oct 22, 2010 (gmt 0)

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



Glad you got it working. The only quick thought with the first script not working in the external JavaScript (PHP) file when it did in the HTML document itself would be if the <SCRIPT> element was still present? Or, if it was called too early, before the 'meta_adtracking' element had loaded in the document? But the same things would affect the 2nd script as well.

tbrim20

3:13 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Talk about chasing my tail... I just re-inserted the old code now that I had it working to see if I could figure out what was wrong and now it works. It must have been a syntax error that I missed in the original copy and paste... The joke's on me.