Getting part of referring url with jquery - javascript

I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew

You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];

Related

javascript window.open url + json var

What I am trying to accomplish is to open a url(base) with an added var at the end.
window.open(https://vipparcel.com/package/detailed/ + json.id[0])
This was written by someone else to just display an image but I need it to pop more detailed info.
I DO NOT know javascript so its like a child poking at a dead animal here.
I have exhausted my normal go to's and then some to find more info on this.
the url would look like https://vipparcel.com/package/detailed/000000 if the info passes correctly.
Anything pointing me in the right direction would be extremely helpful!
Thank you in advanced.
Try this :
var myurl = "https://vipparcel.com/package/detailed/" + json.id[0];
window.open(myurl);
What errors are you getting? Looks like you're just missing quotations. If that's not the case, is json an object with an array of ids? If instead it's an array of objects with an id property, the following should work:
window.open('https://vipparcel.com/package/detailed/' + json[0].id)
It should look like this:
window.open('https://vipparcel.com/package/detailed/' + json.id[0]);
it's looking for a string to open the website by. You weren't passing a string through to it.

Find and replace regex JavaScript

I know this is a super easy question, but I can't seem to wrap my head about it. I've got a bunch of URLs in varying languages such as:
www.myurl.com?lang=spa
www.myurl.com?lang=deu
www.myurl.com?lang=por
I need to create buttons to quickly switch from any language extension (spa, por, deu, rus, ukr, etc) to another language. I have the following code so far:
var url = window.location.toString();
window.location = url.replace(/lang=xxx/, 'lang=deu');
I just can't figure out the 3-character wildcard character. I know that I need to do some sort of regular expression or something, I'm just not sure how to go about it. Any help?
Thanks in advance
You can use
([&?]lang=)\w+
This will work with urls like www.myurl.com?foo=bar&lang=por&bar=foo too.
Instead of lang=deu, you'll have to replace with $1deu.
Try ... or .{3} or \w{3} or even [a-z]{3}, depending on how specific you want to be.
var s = 'www.myurl.com?lang=spa';
s.replace(/lang=[a-z]{3}/, 'lang=deu');
// => "www.myurl.com?lang=deu"
Here's a railroad diagram of the above example:
Use /lang=[a-z][3}/, here's an example:
/lang=[a-z]{3}/
Debuggex Demo

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:
gordon:davis
harley:vona
empir:domen
furno:shyko
I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:
gordon
harley
empir
furno
Is there any simple solution for this?
I have found something called: $.each(split) but I didn't get it work.
I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!
Use String.prototype.split method.
"gordon:davis".split(":") will return ["gordon","davis"]
Based on this, using Array.prototype.map or a JQuery version :
var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
return string.split(":")[0];
});

javascript passing variables via url

Well, question maybe doesn't correct, but I give a shoot.
My script:
OMG, testing
In real, without javascript url look like:
http://localhost/url=google.com
In here wanna add this: &format=txt
So, the correct url then would be:
http://localhost/url=google.com&format=txt
How to add &format to the javascript "script" which showed at the
top?
is this what you want:
OMG, testing
Just add it to the link using +, unless I misunderstood the question.
OMG, testing
Assumes txt is a variable, otherwise put it all in the quotes.
This will do it ,, you have to do same as you did for the localhost
OMG, testing
like that :
OMG, testing
You only have to add the text to the string of your url

Javascript - Get query string from a string?

I'm trying to get the query string from a string (not the current URL).
For example, I have a URL 'www.google.com/?query=string', I'd like to be able to run a function on it and get '?query=string' back from it. How would I go about doing this?
Thanks
Well, you can use a quick regexp that gets you the part you need:
myString.match(/(\?.*)/)[1]
Example:
'www.google.com/?query=string'.match(/(\?.*)/)[1] // evaluates to '?query=string'
Window.location.search will evaluate to this.
http://www.w3schools.com/jsref/prop_loc_search.asp
There's a jQuery plugin for that.
If you're using jQuery, use this plugin: http://projects.allmarkedup.com/jquery_url_parser/
This one lets you operate on the document's url, or any URL string
Then you can do:
$.url.setUrl("www.google.com/?query=string").attr("query") // returns 'query=string'
Or also get a specific parameter:
$.url.setUrl("www.google.com/?query=string").param("query") // returns 'string'
But if you really just need the whole query string, a quick regex like Alsciende suggested is the way to go.

Categories

Resources