Javascript help needed with bookmark - javascript

Hey I have this little javascript bookmark
javascript:(function(){ window.open('http://www.mywebsite.com/index.php?currentsite#bookmark'); })()
How can I get the url of the current website into that url?
I have heard of using document.URL But I am not sure how to get that into the URL in the bookmark with the URL of the site currently browsing. Meaning at the moment the result is http://www.mywebsite.com/index.php?currentsite=document.URL#bookmark
Thanks

javascript:(function(){ window.open('http://www.mywebsite.com/index.php?currentsite=' + encodeURIComponent(location.href) + '#bookmark'); })()

try
window.location or document.location.href or window.location.href
I forgot which one works :)

Try using this instead:
javascript:( function(){window.open('http://www.mywebsite.com/index.php?'+document.location.href+'#bookmark');} )()

Just use window.location.href - like this:
window.open( 'http://<someurl>?' + window.location.href + '#somebookmark' );
window.location.href will give you the href of current frame.

I'm not exactly sure what you are talking about but if you are trying to get the full URL along with the anker you can document.location.href http://www.w3schools.com/jsref/prop_doc_url.asp
You can access the parts of the URL using Location Object http://www.w3schools.com/jsref/obj_location.asp
your code should look like this:
function(){
var url = document.location.href;
window.open('http://www.mywebsite.com/index.php?currentsite = ' + url);
}
Not sure if thats what you where trying to do.

Related

Javascript get url and redirect by adding to it

I have a situation where I need to get the current page url and redirect to a new page by adding to the current page url. For example:
http://www.mywebsite.com/page1/page2
needs to redirect to:
http://www.mywebsite.com/page1/page2/page3
I need it to be relative because "/page1/page2/" will always be different, but "page3" will always be the same.
I've tried: location.href = "./page3"; but that does not work. The result is:
http://www.mywebsite.com/page1/page3
Any thoughts?
Maybe this?:
location.href = location.pathname + "/page3";
Get and Set URL using either window.location.href or document.URL;
window.location.href = window.location.href + "/page3";
Should do what you're looking for.

Javascript/jQuery get root url of website

I have website:
"http://www.example.com/folder1/mywebsite/subfolder/page.html"
Root of the site is: "http://www.example.com/folder1/mywebsite"
I would like to get root url of "mywebsite" dynamicly.
If I will publish this site to another place, then I will no need to change script.
For example to: "http://www.example.com/otherFolder/test/myChangedWebsite/subfolder/page.html"
I will get:
"http://www.example.com/otherFolder/test/myChangedWebsite"
I tried in "page.html" javascript:
var url = window.location.protocol + "//" + window.location.host + '/otherPage.html
but this gives me only "http://www.example.com/otherPage.html".
Also I know about location.origin but this is not supported for all browser as I found that on link: http://www.w3schools.com/jsref/prop_loc_origin.asp
If it can be done with jQuery, it will be good as well.
Here's a function doing the same as Hawk's post above, only much, much shorter:
function getBaseUrl() {
var re = new RegExp(/^.*\//);
return re.exec(window.location.href);
}
Details here: Javascript: Get base URL or root URL
Use the following javascript to get the root of your url:
window.location.origin
For more details:
[https://www.codegrepper.com/code-examples/javascript/javascript+get+root+url][1]
slightly shorter:
function getBaseUrl() {
return window.location.href.match(/^.*\//);
}
if your location is fixed after mywebsite/subfolder/page.html"
then use this
location.href.split("mywebsite")[0]

Javascript opening new window with external URL (Django)

I have some simple lines of code:
that.click(function(){
window.open($('.linkBox input').val());
});
Assuming I'm redirecting to google.com,
whenever a new window is opened, the URL is: "my/project/url/http://www.google.com"
Basically whatever URL is inputted, it gets appended to the end of my project's URL. How can I avoid this?
I think the problem could be the missing http:// in the URL, try this code
that.click(function(){
var url = $('.linkBox input').val();
if (!/^https?:\/\//i.test(url)) {
url = 'http://' + url;
}
window.open(url);
});
Working Demo: http://jsfiddle.net/muthkum/BPBev/1/

Would anyone refactor a URL change better than the way I do?

Not sure if this is inapropriate albeit it working fine.
In order to move to a new location and retain the root URL, I do examples like this :
window.location = window.location.href.split('/oldbar')[0] + '/foobar/' + $value
window.location = window.location.href.split(/oldbar/)[0] + 'foobar';
window.location = window.location.href.split('/oldbar')[0] + '/foobar/'
Would you do it differently? If so how / why?
You don't need to split up the href when you can just access the pathname property of the location object:
// not really sure what you're trying to do, but...
window.location.pathname = window.location.pathname + "/foobar/";
https://developer.mozilla.org/en/DOM/window.location
To retain the root URL, just manipulate window.location.pathname, not the whole href property.

Get relative path of the page url using javascript

In javascript, how can I get the relative path of the current url?
for example http://www.example.com/test/this?page=2
I want just the /test/this?page=2
Try
window.location.pathname+window.location.search
location.href
holds the url of the page your script is running in.
The quickest, most complete way:
location.href.replace(/(.+\w\/)(.+)/,"/$2");
location.href.replace(location.origin,'');
Only weird case:
http://foo.com/ >> "/"
You can use the below snippet to get the absolute url of any page.
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
}
})();
// Sample Result based on the input.
getAbsoluteUrl('/'); //Returns http://stackoverflow.com/
Checkout get absolute URL using Javascript for more details and multiple ways to achieve the same functionality.
I use this:
var absURL = document.URL;
alert(absURL);
Reference: http://www.w3schools.com/jsref/prop_doc_url.asp
You should use it the javascript way, to retrieve the complete path including the extensions from the page,
$(location).attr('href');
So, a path like this, can be retrieved too.
www.google.com/results#tab=2

Categories

Resources