How to add URL ending to "w.location.href" in JavaScript? - javascript

I'm not really bright at JavaScript, so how do I add to "w.location.href"?. I tried searching, but it's hard to formulate the right question too.
This is part of a bookmarklet for Chrome:
encodeURIComponent(w.location.href)
This will return the active URL in the broswer adress field, like http://www.domain.com. But how can I add to it so the ending will be like, "http://www.domain.com/?rcode=abc1234"?
I tried this:
encodeURIComponent(w.location.href + "?rcode=abc12345")
and
encodeURIComponent(w.location.href + ('?rcode=abc12345')
But nothing workes, OR, it droppes out the "=" in the added string.
--
Ok, I'm adding the EDITED "flipit" code here:
javascript:void((function(d,w,p,s,r,t,l)%7Bt%3D(w.screenTop%7C%7Cw.screenY)%2B50%3Bl%3D(w.screenX%7C%7Cw.screenLeft)%2B(w.innerWidth%7C%7Cd.documentElement.offsetWidth%7C%7C0)/2-s/2%3Bw.__flipboard%3Dw.open(%27https://share.flipboard.com/bookmarklet/popout%3Fv%3D2%26title%3D%27%2BencodeURIComponent(d.title)%2B%27%26url%3D%27%2BencodeURIComponent(w.location.href + "?rcode=wlb535")%2B%27%26t%3D%27%2Bp,%27__flipboard_flipit%27,%27width%3D%27%2Bs%2B%27,height%3D%27%2Br%2B%27,top%3D%27%2Bt%2B%27,left%3D%27%2Bl%2B%27,location%3Dyes,resizable%3Dyes,status%3Dno,scrollbars%3Dno,personalbar%3Dno,toolbar%3Dno,menubar%3Dno%27)%3Bs%3Dd.createElement(%27script%27)%3Bs.setAttribute(%27type%27,%27text/javascript%27)%3Bs.setAttribute(%27src%27,%27https://d2jsycj2ly2vqh.cloudfront.net/bookmarklet/js/popout-helper.min.js%3Ft%3D%27%2Bp)%3Bd.body.appendChild(s)%3BsetTimeout(function()%7Bw.__flipboard.focus()%7D,50)%3B%7D)(document,window,(new Date().getTime()),535,565))

Try this:
var url = window.location;
url = encodeURI(url + '?rcode=abc12345');

If you want to change the location programmatically, use:
window.location.href += encodeURIComponent('?rcode=abc12345');

Related

How to trim off the query part of a url using javascript alone?

I've scowerd stackoverflow & for some reason this question is simply not answered in a clear enough manner for me to get right..
I have a url pathname www.thisurl.com/this/url/is/generic%877948
I want to console log the url alone, trimming off the query part of so that all I get is: www.thisurl.com.
I've tried the following:
var pathtotrim = location.href.split('/').pop();
document.write(pathtotrim);
console.log("hello", pathtotrim);
But this trims off the beginning of the url leaving me with /this/url/is/generic%877948.
Essentially doing the opposite of what I want. How can I trim off the query part of a url to be console logged.. please!!!???
Try this:
var pathtotrim = location.href.split('/')[0];
This will split the string on the / but will take the first segment (in your case that would be www.x.com)
Edit:
Like Leilo Faieta Said, location.host or location.hostname would be quicker.
var pathtotrim = "www.thisurl.com/this/url/is/generic%877948".split('/')[0];
document.write(pathtotrim);
console.log("hello", pathtotrim);
let url = 'www.thisurl.com/this/url/is/generic%877948'
console.log(url.replace(/\/.*/g, ''));
There is no need to manipulate strings: you can just use location.host or location.hostname.
This will give you the www part of the url.
on this url:
https://www.google.com/search?safe=off&source=hp&ei=P9SXW7_RO9CKmgWiybjIBA&q=location&oq=location&gs_l=psy-ab.3..0j0i131k1j0l8.269799.273393.0.273746.20.13.4.1.1.0.156.1198.8j4.13.0....0...1c.1.64.psy-ab..2.18.1341.6..35i39k1j0i10k1.98.Zbh8pzpSLkY
you will get
www.google.com
If you want to have all the first part of the url including the http protocol
location.protocol + '//' + location.host
on the same example url you will get
https://www.google.com

How to adjust a link using jQuery or Javascript

What I'm trying to do is to change the last character of a link using jQuery or Javascript.
The link that the client will input into the CMS looks something like this: https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0
What I'd like to do is to set the website to take this link and replace the '0' on the end of the link with a '1'.
Does anyone know how this could be done automatically?
Any help is appreciated,
Tom
As easy as that:
var url = document.getElementById("id of element").href;
url = url.substring(0, url.length-1);
url = url + "1";
document.getElementById("id of element").href = url;
Probably google first about string functions before asking...
If the pattern of the url stays the same (the parameter dl=0) then you can simply use the .replace()-function:
var url = 'https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0';
url = url.replace('dl=0', 'dl=1');

jQuery create and open URL with ampersand

I'm trying to build an URL in jquery and then open it. The problem is that the URL should contain an ampersand and I can't figure out how to do that
var url = 'print?month=' + jQuery('#month').val() + '&year=' + jQuery('#year').val();
var encodedUrl = encodeURIComponent(url);
window.open(encodedUrl);
Essentially, what I'd like the URL to be is: print?month=1&year=2015
Yes, changing & a m p ; [without spaces] to & should work, if not you should probably do something like below:
var url = 'print?month=' + encodeURIComponent(jQuery('#month').val()) + '&year=' + encodeURIComponent(jQuery('#year').val());
window.open(url);
Fully working plnkr is at:http://plnkr.co/edit/70h1E8SJDXMhtMJPnKMp?p=preview
Just enable popups on your browser for plnkr if you dont see the popup yet.
Basically just create a string with concatenating & charecters, its just a string, you dont need to URIEncode it, then do a window.open of the string. That should do it.

How to remove some part of the URL in the address bar of the browser

I try to remove a part of my url in the addressbar of the browser via javascript.
but I don't understand why it's not working, if I test it in the console the result is correct but it still does not change in the address bar.
How can I do it?
url I have:http://localhost:8090/Home/Index?x=72482&success=itsdone
url I want is:
http://localhost:8888/Home/Index?x=72482
here is my javascript code:
window.location.href.replace('&', '#');
window.location.hash = "";
replace doesn't change the string on which you call it (strings are immutable), it returns a new one.
To replace & with #, do
window.location = window.location.href.replace('&', '#');
If you want to remove everything from the first &, the best is to use a regular expression :
window.location = window.location.replace(/&.*$/,'');
If what you want is to retain the x parameter, then you should rebuild the location to ensure it's still OK if the parameters are in a different order in the URL :
window.location = window.location.replace(/([^?]*).*(\?|&)(x=)([^&]+).*/, "$1?$3$4")
This changes
"http://localhost:8888/Home/Index?a=2&x=72482&c=3"
or
"http://localhost:8888/Home/Index?x=72482&success=itsdone"
into
"http://localhost:8888/Home/Index?x=72482"
window.location will cause a page reload. to change the port too use this
window.location = window.location.protocol
+ "//"
+ window.location.host
+ ":8888/"
+ window.location.pathname
+ window.location.search.substr(0, window.location.search.indexOf('&')-1)
Is there a particular reason why you are passing isDone as a QueryString parameter if you do not even need it? Wouldn't it be easier to not even pass it to begin with and have the page decide if you are done?

How do I trim/strip the URL down to the page name?

How would I go about trimming/stripping the URL down to the page name...
So: http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html
Would become: apage.html
Any ideas?
you do not need jquery:
var url = window.location.href;
var page = url.substring(url.lastIndexOf('/') + 1);
Edit: a good point of the possible query string:
// it might be from browser & / anywhere else
var url = window.location.href;
url = url.split('#').pop().split('?').pop();
var page = url.substring(url.lastIndexOf('/') + 1);
ok, if the location object is available, use pathname gives better result as show below, however, a url can be a string or something directly from text field or span/label. So above solution should have its place.
With location and any link (<a>) elements on the page, you get a load of properties that give you specific parts of the URL: protocol, host, port, pathname, search and hash.
You should always use these properties to extract parts of the URL in preference to hacking about with href and probably getting it wrong for corner cases. For example, every solution posted here so far will fail if a ?query or #fragment is present. The answers from Rob and digitalFresh attempt to cope with them, but will still fail if a / character is present in the query string or fragment (which is valid).
Instead, simply:
var pagename= location.pathname.split('/').pop();
Most of the solutions here are not taking advantage of the window.location object. The location object has this wonderful thing called pathname which returns just the path, no query string, host, protocol, hash, etc.
var mypage = window.location.pathname.split("/").pop();
You could do something like this:
document.location.href.split('/').pop();
Edit: you probably want to get rid of the query string if there is one also:
document.location.href.split('/').pop().split('?').shift();
Edit 2: this will also ignore an anchor in the url if there is one
document.location.href.split('/').pop().split(/\?|#/).shift();
This should also exclude query and hash values.
var path = location.href;
path = path.substring(path.lastIndexOf("/") + 1);
path = path.split("?")[0].split("#")[0];
console.debug(path);
Haven't tested so compeltely guessed, but I'm sure something like this will do :-)
var url = 'http://www.BurtReynoldsMustache.com/whatever/whoever/apage.html';
var page = url.split('/');
alert(page[page.length-1]);​
EDIT Tested under jsfiddle and it was wrong, the above code should now work :-)

Categories