在 window.location.href 中使用变量
我想知道如何在 javascript 中执行这样的命令。我只想将 url 作为字符串存储在变量中,并在需要时将它们重定向到该变量 -
var linkz = "http://www.google.com/";
window.location.href= linkz;
为什么这不起作用?
谢谢,
I was wondering how I would be able to execute a command such as this in javascript. I just want to store the url as a string in a variable and redirect them to it when the time comes -
var linkz = "http://www.google.com/";
window.location.href= linkz;
Why doesn't this work?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您以这种方式使用链接(如评论中所述):
那么问题是您没有将字符串传递给
checkCk()
函数。试试这个:
您用于
window.location.href
的代码应该可以工作。此时,如果您所做的只是替换链接的默认行为,我不明白为什么您要使用 javascript。
If you're using links this way (as mentioned in a comment):
Then the problem is that you're not passing a string to your
checkCk()
function.Try this:
The code you used for
window.location.href
should work.At this point, I don't see why you'd use javascript if all you're doing is replacing the default behavior of the link.
我刚刚在本地计算机上尝试过,它可以工作:
将其复制到新文件,将其命名为
redirect.html
并在浏览器中打开它。更新:
I've just tried on my local machine, and it works:
Copy this to new file, call it for example
redirect.html
and open it in your browser.Update: