如何在javascript中的变量中存储url?
我正在尝试这个粗略的脚本想法。但它不起作用如何在javascript中的变量中存储url?,javascript,url,onclick,Javascript,Url,Onclick,我正在尝试这个粗略的脚本想法。但它不起作用 <script> function storeurl() { var varurl = document.URL; } document.onclick = storeurl; document.write(varurl); </script> 当点击链接时 有什么帮助吗? Thx您的varurl变量的作用域在方法(函数)级别。这意味着它对在函数外部运行的代码不可见 var varurl; function storeur
<script>
function storeurl() {
var varurl = document.URL;
}
document.onclick = storeurl;
document.write(varurl);
</script>
当点击链接时
有什么帮助吗?
Thx您的varurl变量的作用域在方法(函数)级别。这意味着它对在函数外部运行的代码不可见
var varurl;
function storeurl() {
varurl = document.URL;
}
此外,document.write代码将在脚本首次运行时执行,即在单击之前(如果单击发生)
如果不需要使用varurl而只需将其写入文档,则可以将document.write代码移动到函数中,并保留varurl的狭窄范围:
<script>
function storeurl() {
var varurl = document.URL;
document.write(varurl);
}
document.onclick = storeurl;
</script>
函数storeurl(){
var varurl=document.URL;
document.write(varurl);
}
document.onclick=storeurl;
否则,将变量定义移出函数,使其(变量)成为全局变量:
<script>
var varurl;
function storeurl() {
varurl = document.URL;
document.write(varurl);
}
document.onclick = storeurl;
</script>
var-varurl;
函数storeurl(){
varurl=document.URL;
document.write(varurl);
}
document.onclick=storeurl;
var使其成为函数作用域的局部变量。此外,您还试图在设置之前读取它。您已将varurl
定位到使用var
声明的函数中,因此从该函数外部看不到它
var varurl;
function storeurl() {
varurl = document.URL;
}
您还将立即写入它的值,而不是在单击事件中写入,因此在您write()
时它不会被设置
将代码更改为
var varurl;
function storeurl() {
varurl = window.location.href;
}
应该行得通,
<script>
function storeurl() {
varurl = document.URL; // it should be Global variable, so remove var
document.write(varurl);//when you're declaring this outside of the function
}
document.onclick = storeurl;
</script>
函数storeurl(){
varurl=document.URL;//它应该是全局变量,所以删除var
document.write(varurl);//当您在函数外部声明时
}
document.onclick=storeurl;
为了简单地将URL存储在变量中,可以是外部URL或当前页面的URL,然后显示该URL或对其执行其他操作,您可以按照以下代码所示操作:
<html>
<body>
<button onclick="go()">GO TO GOOGLE</button><br/>
<button onclick="show()">CLICK TO SHOW CURRENT URL</button><br/>
<p id="showhere"></p>
<script>
function go(){
var u = "http://www.google.com" ;
window.location.href = u; //takes you to google.com
}
function show(){
var x = window.location.href;
document.getElementById("showhere").innerHTML = x;
//shows URL of current page below the buttons
}
</script>
</body>
</html>
转到谷歌
单击以显示当前URL
函数go(){
变量u=”http://www.google.com" ;
window.location.href=u;//带您访问google.com
}
函数show(){
var x=window.location.href;
document.getElementById(“showhere”).innerHTML=x;
//在按钮下方显示当前页面的URL
}
那么,它有什么作用?它没用吗?犯错误?使你的电脑崩溃?让飞行的猴子从天而降?给你免费的华夫饼干?请澄清。varurl
是私有的,您不能从storeurl
函数中的本地范围之外访问它。也不是document.onclick=storeurl;一个问题,因为它应该是:document.onclick=storeurl()?门把手为什么这么刻薄?是的,我在删除之前取出了var是的,这很好,我添加了文档;它返回url/TheNameofScript.php,但不返回包含href的内容,在本例中,#2.document.url将为您提供当前文档的url。单击某个内容时是否要重定向到#2?请详细说明。
<html>
<body>
<button onclick="go()">GO TO GOOGLE</button><br/>
<button onclick="show()">CLICK TO SHOW CURRENT URL</button><br/>
<p id="showhere"></p>
<script>
function go(){
var u = "http://www.google.com" ;
window.location.href = u; //takes you to google.com
}
function show(){
var x = window.location.href;
document.getElementById("showhere").innerHTML = x;
//shows URL of current page below the buttons
}
</script>
</body>
</html>