Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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/8/swift/18.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_Jquery_Redirect_Parameter Passing - Fatal编程技术网

如何使用javascript将任何外部链接作为url参数传递?

如何使用javascript将任何外部链接作为url参数传递?,javascript,jquery,redirect,parameter-passing,Javascript,Jquery,Redirect,Parameter Passing,我对javascript一无所知 我有一个网站mysite.com,有大量的外部链接,我试图跟踪每个外部链接,并将其作为参数附加到外部域,如 myanothersite.com/redirect.html?url=` 使用javascript myanothersite.com/redirect.html是一个页面,它会在5秒钟后重定向到传递的url参数,我已经成功创建了这个页面 简而言之,当访问者点击外部链接时,我不希望他直接访问外部站点。相反,我想这样做 mysite.com -->

我对javascript一无所知

我有一个网站
mysite.com
,有大量的外部链接,我试图跟踪每个外部链接,并将其作为参数附加到外部域,如

myanothersite.com/redirect.html?url=`

使用javascript

myanothersite.com/redirect.html是一个页面,它会在5秒钟后重定向到传递的url参数,我已经成功创建了这个页面

简而言之,当访问者点击外部链接时,我不希望他直接访问外部站点。相反,我想这样做

mysite.com 
--> Visitor clicks on http://externallink.com 
--> myanothersite.com/redirect.html?url=http://externallink.com 
--> http://externallink.com
所以,这里我尝试编写一个js代码,自动检测外部链接并将其作为查询参数附加到

myanothersite.com/redirect.html?url=


npm上有一个包可以帮助您

此外,。窗口对象是一个全局对象,可以处理重定向、打开新窗口或重新加载。所以你可以看一下文件,也可以看一下那个

如果您正在寻找一个更简单的解决方案,您可以试试这个

像这样从窗口对象抓取链接并存储在变量中

var location = window.location.href
然后像这样将其拆分为“url=”

var splitArray = location.split('url=');
console.log(splitArray[1]);

您应该在控制台中看到您的url。您可以在任何地方使用它

“所以,我在这里尝试编写js代码”-好的,让我们看看您当时尝试了什么,使用url编码
$(function(){$([href^=http])。在(“单击”上,function(e){e.preventDefault();location=“myanothersite.com/redirect.html?url=“+encodeURIComponent(this.href);});})您没有正确阅读问题。