PDA

View Full Version : Java pop-up window



Dudaa
09-27-2009, 05:04 AM
I'm not really good with javascript and need help with a problem. I'm creating a pop-up window that goes to another site but sends the url it's coming from in the url it's going to.

Ok here's what I have if that's confusing.

<head>
<script type="text/javascript">
function popupwin(url)
{
var width = 650;
var height = 250;
var left = (screen.width - width)/2;
var top = (screen.height - height)/2;
var params = 'width='+width+', height='+height;
params += ', top='+top+', left='+left;
params += ', scrollbars=1';
newwin=window.open(url,'popupwin', params);
if (window.focus) {newwin.focus()}
return false;
}
</script>
</head>
<body>
<a href="javascript: void(0)" onClick="popupwin('http://example.com/check.php?id=http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]'); return false;">Centered popup window</a>
</body>

Now that above code works fine in most cases and on most sites. The problem is that one forum I've tried it on has a seo mod which means the php comand $_SERVER[REQUEST_URI] isn't the same that's in the address bar. Instead of showing
http://sitename.com/forum/main-forum/1-test-post-title-some-list.html
it's showing
http://sitename.com/forum/showthread.php?p=1

After Googling I know that document.write(location.href) will give me the exact url of the seo url as it appears in the address bar which is what I want. Can anyone show me how to combine document.write(location.href) with my code. I know it's probably easy for most but I only really know basic html and php and I'm only new to javascript.

Thanks in advance.

jscheuer1
09-27-2009, 07:05 AM
The document.write() method should be avoided in most cases, all really after page load, as at that point it will overwrite the page.

Also, location.href will not give you anything other than what appears in the address bar.

Anyways, I'm not sure exactly what you are going for, but here's what I would try:


<a href="javascript: void(0)" onclick="popupwin('http://example.com/check.php?id=' + encodeURIComponent(location.href)); return false;">Centered popup window</a>


I use the encodeURIComponent() method to avoid messiness/errors that can occur if a URL with its own query component is used as a part of a query in another URL. If you need this information decoded on the other end (in this case at 'example.com'), you may use the decodeURIComponent() method at the receiving end there to get it to look like it did in the address bar.