【问题标题】:How to remove ‘&m=0’ which automatically getting attached as suffix to url如何删除自动作为后缀附加到 url 的“&m = 0”
【发布时间】:2018-08-08 20:07:53
【问题描述】:

这件事只发生在移动设备上。 例如 www.example.com&m=0

我正在使用脚本将我的所有链接重定向到一个特定页面,该页面是一个确认页面,用户在点击“确认”时只有他们被重定向。 www.santhaledisom.com/p/confirmation.html?=‘www.yourcontent.com’ 只有在点击“确认”按钮后,它们才会被重定向到 www.mycontent.com。

但它实际上似乎是“www.mycontent.com&m=0”,因此链接不起作用

这东西在桌面版本上运行良好,但在移动设备上使用时却不行。 我的网站是基于 Blogger 平台的,即使关闭了 Blogger 的移动模板,它仍然会发生这种情况。

Confirmation.html 页面有按钮 (Id=myButton)

    <script>
   //get a reference to the element 
    var myBtn = document.getElementById('myButton'); 
    var href = document.location.href; 
    var link = href.split('?=')[1]; 
    //add event listener 
    myBtn.addEventListener('click', function(event)
    { 
    window.location.href="http://" link; });

</script>

我猜这可能是在移动版和桌面版之间切换的东西 并将“m=0”添加到我的所有网址。

 var curl = window.location.href;if (curl.indexOf('m=1') != -1) {curl = curl.replace('m=1', 'm=0');window.location.href = curl;

【问题讨论】:

  • 您的第二个代码不完整,请提供完整代码。
  • 这是我发现的
  • 我添加了一个答案。
  • 如果您正确解析查询字符串(即 url 中 ? 之后的位),那么您不应该遇到这个问题。我注意到您似乎没有 url 参数的名称,如果您这样做了(例如,url 是 www.santhaledisom.com/p/confirmation.html?link=www.yourcontent.com,然后将变为 www.santhaledisom.com/p/confirmation.html?link=www.yourcontent.com&amp;m=0 并且当您从查询字符串中获得 link 参数时,您仍然会获取正确的 url,无论有多少查询字符串元素添加到 url。
  • @Chris 谢谢,我明白了。我的问题解决了。

标签: javascript html blogger


【解决方案1】:

首先,您不能从移动设备的链接中删除m=1m=0,这是所有博客博客的强制性要求。

您的重定向链接应该有一个查询名称,您应该将前缀脚本中的重定向链接从 ?='www.yourcontent.com' 更改为类似 ?link=www.yourcontent.com 的名称。

现在,如果您有这样的链接: /p/confirmation.html?link=www.yourcontent.com&amp;m=0,您可以轻松提取目标链接,无需删除m=1m=0,使用这些简单的代码行:

var myBtn = document.getElementById('myButton'),
    TargetLink;

location.search.substring(1).split('&').forEach(function(par){
    var query = par.split('=');
    if(query[0]==='link'){ TargetLink = query[1] }
});

myBtn.addEventListener('click', function(){
    window.location.href = location.search ? 'http://' + TargetLink : '#';
});

【讨论】:

  • 感谢@Muhammad Saleh,它很有魅力,您帮助 stackoverflow 再次为开发人员世界的其他人证明了这一点。
【解决方案2】:

用空字符串替换m=0

var curl = window.location.href;
if (curl.indexOf('m=1') != -1) {
  curl = curl.replace('m=1', '');
  window.location.href = curl;
}

【讨论】:

  • 您的意思是在该代码中使用m=0 而不是m=1
  • 是 m=0,经过多次实验,我知道我们无法删除它。删除后博客无法正常运行。
猜你喜欢
  • 1970-01-01
  • 2019-05-21
  • 2016-10-09
  • 1970-01-01
  • 2018-11-02
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多