javascript window.location.assign("link"), 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16398690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location.assign("link"), is not working
提问by Swaroop Nagendra
Here is the javascript code.
这是javascript代码。
<script type="text/javascript">
function myfunc()
{
var filmName = document.getElementById("mysearch-text").value;
alert(filmName);
if(filmName == "parker")
?window.location.assign("http://www.google.com");
else
alert("hello");
}
</script>
If I enter any other string I get an "hello" alert and If I enter "parker" the page "http://www.google.com" wont get loaded. Why is this happening?
如果我输入任何其他字符串,我会收到“你好”警报,如果我输入“parker”,则页面“ http://www.google.com”不会被加载。为什么会这样?
EDIT: Ok as someone mentioned I did remove "http://www.google.com" and added "http://stackoverflow.com" but it did not resolve my problem. And I cant even load local html pages that are in the same directory
编辑:好的,正如有人提到的,我确实删除了“ http://www.google.com”并添加了“ http://stackoverflow.com”,但它没有解决我的问题。我什至无法加载同一目录中的本地 html 页面
if(filmName == "parker")
window.location.assign("index.html"); // also "./index.html" or ./index/html
Even this is not working. What's wrong
即使这是行不通的。怎么了
I also have jquery libraries: that is Jquery UI and Jquery
我也有 jquery 库:即 Jquery UI 和 Jquery
This question needs more of info I think. Here are the final edits
我认为这个问题需要更多信息。这是最终的编辑
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Other style and js libraries, including Jquery Ui and regular jquery -->
<script>
$(document).ready(function() {
$(".youtube").fancybox();
}); // end ready
</script>
<script>
$(document).ready(function(e) {
$('.tooltip').hide();
$('.trigger').mouseover(function() {
/* Rest of it, which is not necessary here */
</script>
<script type="text/javascript">
function myfunc()
{
var filmName = document.getElementById("mysearch-text").value;
alert(filmName); // for debugging
if(filmName == "parker")
?window.location.assign("index.html");
else
alert("hello");
}
</script>
</head>
<body>
<div id='mysearch-box'>
<form id='mysearch-form'>
<input id='mysearch-text' placeholder='' type='text'/><!-- Input here guys -->
<button class = "none" id='mysearch-button' onclick = "myfunc()">Search</button>
<!-- press this button after entering "parker" -->
</form>
</div>
<!-- Rest of the HTML page -->
</body>
</html>
回答by Mohamed-Ted
I assume you are using a form onsubmit
event to call myfunc
, if so you have to prevent the default behavior by return false;
(for example)
我假设您正在使用表单onsubmit
事件来调用myfunc
,如果是这样,您必须通过return false;
(例如)防止默认行为
HTML:
HTML:
<form id="form">
<input type="text" id="mysearch-text">
<input type="submit" value="Submit">
</form>
JS:
JS:
<script>
window.onload = function () {
document.getElementById("form").onsubmit = function () {
var filmName = document.getElementById("mysearch-text").value;
alert(filmName);
if (filmName == "parker") window.location.href = "http://www.w3fools.com";
else alert("hello");
return false; //prevent the default behavior of the submit event
}
}
</script>
回答by Juanma García
I have tested the following solution and it works perfectly:
我已经测试了以下解决方案,它运行良好:
setTimeout(function(){document.location.href = "page.html;"},500);
setTimeout(function(){document.location.href = "page.html;"},500);