Get Parent URL With Javascript


I frequently have to create child window and the child windows sometimes need its parent URL, in javascript just create a variable to store the url. In this example I copy the content of parentURL variable to a hidden form element named sourceURL

<script type="text/javascript">
function frmSubmit(){
    parentURL = opener.location.href;
    document.getElementById('sourceURL').value = opener.location.href;
    document.forms['myform'].submit();
}
</script>
<form name="myform" id="myform" method="post" action="target.php">
<input type="hidden" name="sourceURL" id="sourceURL" />
<input type="button" value=" submit " onclick="frmSubmit()" />
</form>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s