将location.href拆分为多个值? [英] Split location.href for multiple values?

查看:312
本文介绍了将location.href拆分为多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,它可以找到当前网址并将其拆分如下:

I have a varient that finds the current url and splits it as follows:

var ehref = window.location.href.split('?',1);

然后将其用于将URL与导航链接href匹配,并为页面提供ID.我的问题是,当我们关闭Cookie弹出窗口时,#添加到了网址中.随后,页面链接在用户之间传递,并且#和页面ID不起作用.

This is then used to match the url with a navigation link href and give an ID to the page. My issue is that when our cookie pop up is closed, # is added to the url. Subsequently the page links are passed around between users with the # and the page ids do not work.

在#处分割网址的简单方法是什么?我是jquery的新手,因此我了解自己在阅读"的要旨,但是我在研究网络时所做的任何尝试都破坏了页面.我可以代替?"使用#",但这并不能真正解决问题.

What is a simple way of splitting the url at a # as well? I am new to jquery, thus I understand the gist of what I'm 'reading,' but anything I've tried from researching the net has broken the page. I can replace the '?' With '#' but that doesn't really solve the issue.

谢谢!

推荐答案

我一直在寻找拆分URL并替换为新URL的方法.例如YouTube.com/用户/视频",并将其更改为YouTube.com/v/视频",这样我就不必登录即可观看受限制的视频.但随后需要使用相同的代码来捕获两个标记之间我想要的任何字符串.所以我们开始吧!

I have been searching for a way to split up a URL and replace with a new URL. as example, YouTube.com/ "user/video" and change it to YouTube.com/v/"video" so I would not have to sign in to watch a video that got restricted. But then needed to use the same code that would grab whatever string I want between two marks. So here we go!

我们的目标:隔离URL的一部分并在另一个URL中使用它! 代码行将分成几部分,以方便阅读 该行代码用于网络链接,请从浏览器的书签中单击

Our goal: To isolate a part of a URL and use it within another URL! The line of code will be broken up in sections for easy reading The line of code will be for a web-link, clicked from the browser’s bookmark

示例网址

https://duckduckgo.com/?q=School&t=h_&atb=v102-5_f&ia=web

https: //duckduckgo.com/?q=School&t=h_&atb=v102-5_f&ia=web

代码:

javascript:var DDG=(window.location.href.split('?q=')[1]);DDG2=DDG.split('&t')[0];DD2G="https://www.google.com/search?q="+DDG2;window.location.assign(DD2G);

变量名;

  • DDG = duckduckgo
  • DDG2 = duckduckgo2
  • DD2G = duckduckgo 2 Google

代码分解:

  1. javascript:var DDG =(window.location.href.split('?q =')[1]);

  1. javascript:var DDG=(window.location.href.split('?q=')[1]);

DDG2 = DDG.split('& t')[0];

DDG2 = DDG.split('&t')[0];

DD2G ="https://www.google.com/search?q=" + DDG2;

DD2G="https://www.google.com/search?q="+DDG2;

代码的第一部分将其定义为JavaScript,我们创建了一个名为DDG的变量(var)

The first part of the code defines it as a JavaScript, we create a variable (var) with the name DDG

Var DDG

下一部分,我们希望该值是用户浏览器的当前URL,并将其分为几部分

The next part we want the value to be what the current URL of the users browser and split that into sections

window.location.href.split

我们想在URL中找到字符串?p =",该字符串指示在duckduckgo中进行的搜索查询.

We want to find within the URL this string ‘?p=’ which indicates the search inquiry/s in duckduckgo

但是我只想用[1]表示的'?p ='之后是什么,它将为我们的变量名DDG提供以下值:School& t = h_& atb = v102-5_f& ia = web 现在,我们想分割刚刚赋予DDG变量的新值,因此我们对此进行了分割

But I only want what comes after ‘?p=’ represented by [1], which will give our variable name DDG the value of this: School&t=h_&atb=v102-5_f&ia=web We now want to split the new value we just gave to our DDG variable, so we do a split on that

DDG.split,这次我们只需要'& ='之前的所有内容,因此我们将[0]放入结果并将其分配给名为DDG2的新变量

DDG.split, and this time we only want everything before the ‘&=’ so we put [0] and assigned that result to a new variable we called DDG2

DDG2 = DDG.split(‘&t’)[0]

我们现在有了一个具有所需值的新变量,我们将使用DDG2替换另一个URL中想要的任何内容!

We now have a new variable with the value we wanted and we will use DDG2 to replace whatever we want in another URL!

DDG2 =学校(每次进行新搜索时都会更新.)

DDG2 = School (this updates every time there is a new search.)

现在,我们要用新的URL +变量名替换URL. 我们将最终变量名称DD2G设为以下值:https://www.google .com/search?q =,但我们想从DDG2中添加我们的值

Now we want to replace the URL with our new URL + our variable name. We make our final variable name DD2G with the value of: https:// www.google .com/search?q= but we want to add our value from DDG2

DD2G="https: //www.google.com/search?q="+DDG2;

看起来像这样(https://www.google.com/search?q=School). 现在,我们希望将其分配给浏览器,它将使用搜索词重定向到新的URL.

Which would look like this (https: //www.google.com/search?q=School). We now want to assign that to the browser and it will redirect to the new URL with the search term.

window.location.assign(DD2G);

window.location.assign(DD2G);

= window.location.assign("https://www.google.com/search?q=" +(DDG2))

= window.location.assign("https: //www.google.com/search?q=" + (DDG2))

= window.location.assign("https://www.google.com/search?q=学校")

= window.location.assign("https: //www.google.com/search?q=School")

= https://www.google.com/search?q=学校//我们的新URL带有我们从duckduckgo开始的搜索词,而无需重新输入查询.

= https: //www.google.com/search?q=School //our new URL with our search term we started with from duckduckgo, without having to retype the inquiry.

因此,对于您的问题,只需将``'?q ='之间的字符串替换为您要脚本查找的第一个字符串,然后从该结果中,将''& t'之间的第二个字符串更改为您要查找的第二个字符串.

So for your question, just replace the string between '' '?q=' with the first string you want the script to look for, then from that result, change the second string between'' '&t' with the second string you want it to look for.

我希望这会有所帮助!

如果要对其进行测试,请选择以下所有选项:

if you want to test it out select all of this:

javascript:var DDG=(window.location.href.split('?q=')[1]);DDG2=DDG.split('&t')[0];DD2G="https://www.google.com/search?q="+DDG2;window.location.assign(DD2G);

并将其拖动到工具栏/书签中的空白处,在Firefox中,我不知道它是否可以与其他浏览器一起使用,但是如果它们支持JavaScript,则应该可以使用.现在,导航至DuckDuckgo.com并搜索内容,然后单击带有该代码的书签.

and drag it to an empty space in your toolbar/bookmarks, in Firefox, I do not know if this works with other browsers, but if they support JavaScripts, it should work. Now navigate to DuckDuckgo.com and search for something, then click on that bookmarked with that code.

这篇关于将location.href拆分为多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆