Split location.href for multiple values?

跟風遠走 提交于 2019-12-12 05:51:44

问题


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

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

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.

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.

Thanks!


回答1:


If you want to get string after '#' you can write like this:

window.location.hash

in javascript ,see here




回答2:


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!

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

Example URL

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

The code:

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);

Variable name;

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

The code break down:

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

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

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

  4. window.location.assign(DD2G);

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

Var DDG

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

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

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, 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]

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 = School (this updates every time there is a new search.)

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;

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(“https: //www.google.com/search?q=” + (DDG2))

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

= 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.

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.

I hope this helps!

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);

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.



来源:https://stackoverflow.com/questions/30070147/split-location-href-for-multiple-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!