Get current webpage URL [duplicate]

2019-09-07 05:45发布

问题:

Possible Duplicate:
Request Address in Javascript

I am using a Facebook plugin in my web page, as shown:

<iframe 
   src="http://www.facebook.com/plugins/like.php?href=YOUR_URL"
   scrolling="no" 
   frameborder="0"
   style="border:none; width:120px; height:30px">
</iframe>

How can I get the current URL of the web page, using JavaScript? I need to assign current URL instead of href=YOUR_URL. How can I achieve this?

回答1:

Just don't put an href parameter. Facebook will default to the current page.



回答2:

Use this:

window.location.href (or simply location.href)

Definition of location.href



回答3:

jQuery is your friend here:

var original = $("iframe").attr('src');
var url = window.location;
$("iframe").attr('src', original + url);​

HTML

<iframe src="http://www.facebook.com/plugins/like.php?href="></iframe>​

http://jsfiddle.net/charlescarver/2JEay/