Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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在正文中搜索关键字,如果找到,重定向到特定的URL_Javascript_Search_Redirect - Fatal编程技术网

Javascript在正文中搜索关键字,如果找到,重定向到特定的URL

Javascript在正文中搜索关键字,如果找到,重定向到特定的URL,javascript,search,redirect,Javascript,Search,Redirect,我试图使用JavaScript在页面正文中搜索特定的关键字 如果关键字出现在正文中,我需要立即将页面重定向到指定的URL。如果关键字没有出现在正文中,则不会发生任何事情 我试过这样的方法: function redirect() { var thing = $('body:contains("keyword")').text(); if(thing) { window.location.replace("http://websiteurlhere"); } } 不用说,我运

我试图使用JavaScript在页面正文中搜索特定的关键字

如果关键字出现在正文中,我需要立即将页面重定向到指定的URL。如果关键字没有出现在正文中,则不会发生任何事情

我试过这样的方法:

function redirect() {
var thing = $('body:contains("keyword")').text();
   if(thing) {
    window.location.replace("http://websiteurlhere");
   }
}

不用说,我运气不好。这是一个奇怪的要求,但如果有人能给我指出正确的方向,我将不胜感激

在需要的地方调用函数,如果找到关键字,则使用.attr重定向

function redirect() {
var thing = $('p:contains("keyword")').text();
   if(thing) {
       $(location).attr('href',"http://websiteurlhere");//JQuery redirect
   }
}
redirect();//Call your function
如果您绝对希望使用JavaScript而不是Jquery重定向,可以使用此选项而不是.attr:


这很有效。我似乎要做的唯一调整是使用$'p:containskeyword',因为我要搜索的文本是在标记之间,而不是使用$'body:containskeyword'。使用$'body:containskeyword'并没有返回结果,但当我将其缩小到$'p:containskeyword'时,它工作得非常好。知道为什么吗?@Vivi_555我已经用$'p:containskeyword'>更新了我的答案。我不能告诉你为什么没有你的HTML。你能点击勾号把问题解决吗?
document.location.href="http://websiteurlhere";