Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.location.assign(“链接”)不起作用_Javascript_Html_Css - Fatal编程技术网

Javascript window.location.assign(“链接”)不起作用

Javascript window.location.assign(“链接”)不起作用,javascript,html,css,Javascript,Html,Css,下面是javascript代码 <script type="text/javascript"> function myfunc() { var filmName = document.getElementById("mysearch-text").value; alert(filmName); if(filmName == "parker")  wi

下面是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>
即使这样也不行。怎么了

我还有jquery库:即jQueryUI和jquery

我认为这个问题需要更多的信息。这里是最后的编辑


$(文档).ready(函数(){
$(“.youtube”).fancybox();
}); // 准备就绪
$(文件).ready(函数(e){
$('.tooltip').hide();
$('.trigger').mouseover(函数(){
/*其余部分,此处不需要*/
函数myfunc()
{
var filmName=document.getElementById(“mysearch文本”).value;
警报(filmName);//用于调试
如果(filmName==“parker”)
window.location.assign(“index.html”);
其他的
警惕(“你好”);
}       
搜寻

我假设您正在使用表单
onsubmit
事件调用
myfunc
,如果是这样,您必须通过
返回false来防止默认行为;
(例如)

HTML:


JS:


window.onload=函数(){
document.getElementById(“表单”).onsubmit=function(){
var filmName=document.getElementById(“mysearch文本”).value;
警报(电影名称);
如果(filmName=“parker”)window.location.href=”http://www.w3fools.com";
else alert(“你好”);
return false;//防止提交事件的默认行为
}
}        

我已经测试了以下解决方案,它工作得非常好:


setTimeout(function(){document.location.href=“page.html;”,500);

第一个
alert()
输出的是什么?您确定要请求具有此属性的元素的值吗?如果我的输入字符串是“parker”,则alert输出“parker”但是不加载google.com。但是如果我的输入是除“parker”之外的任何其他字符串,那么“hello”是警报,如果页面没有阻止加载到框架中,那么警报似乎可以正常工作。您可以告诉我您使用的浏览器是(a)使用某种简单/愚蠢的迷你器还是(b)尝试引用其他窗口吗(可能是一个iframe或一个window.open返回的window对象)?控制台怎么说?我很确定有什么遗漏了。
if(filmName == "parker")
                 window.location.assign("index.html"); // also "./index.html" or ./index/html
<!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>
<form id="form">
    <input type="text" id="mysearch-text">
    <input type="submit" value="Submit">
</form>
<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>