Get the full url including hash with content in JavaScript - javascript

How can I get url with content after hash ?
window.location return me url without hash :/
for example:
www.mystore.com#prodid=1
window.location return only www.mystore.com

window.location.hash
https://developer.mozilla.org/docs/Web/API/Window/location
Note the properties section.

Try window.location.hash this will work

this returns just content after hash
window.location.hash.substr(1);
ex: www.mystore.com#prodid=1
this will give us : prodid=1

If you only want the hash part you can use : window.location.hash
If you want all the url including the hash part, you can use : window.location.href
Regards

You have to build it up yourself:
// www.mystore.com#prodid=1
var sansProtocol = window.location.hostname
+ window.location.hash;
// http://www.mystore.com#prodid=1
var full = window.location.protocol
+ "//"
+ window.location.hostname
+ window.location.hash;

Related

How can I put something at the end of the URL using jQuery upon clicking a dropdown? [duplicate]

Is there is a way how to add hash # to my URL without redirect?
window.location.hash = 'something';
That is just plain JavaScript.
Your comment...
Hi, what I really need is to add only the hash... something like this: window.location.hash = '#'; but in this way nothing is added.
Try this...
window.location = '#';
Also, don't forget about the window.location.replace() method.
For straight HTML, with no JavaScript required:
Add '#something' to URL
Or, to take your question more literally, to just add '#' to the URL:
Add '#' to URL
window.location.hash = 'whatever';
Try this
var URL = "scratch.mit.edu/projects";
var mainURL = window.location.pathname;
if (mainURL == URL) {
mainURL += ( mainURL.match( /[\?]/g ) ? '&' : '#' ) + '_bypasssharerestrictions_';
console.log(mainURL)
}

change part of the url text and reload page

Is there a way to make an a tag link that includes part of the current url?
I would use it to change language but stay on the current page!
If I am on:
www.mysite.com/contact
and I press on:
<a>FR</a>
the generated link would be:
www.mystie.com/fr/contact
Kind regards
Try this:
HTML:
FR
Now get the url from javascript
$("#myLink").click(function(e){
e.preventDefault();
var url = document.url; // get current page url
url = url.substr(0, url.indexOf('/')); // get substring from url
url += "/" + $(this).attr("href"); // add href of a tag to url
window.location.href = url; // locate the page to url
});
you can try with some like
function changeLng(lang) {
// remove language '/fr/' or '/es/' if exists
var path = window.location.pathname.replace(/\/[a-z]{2}/, '');
// reload the page same url with the lang prefix
window.location.href = '/' + lang + path;
}
the link can be
FR
ES
EN
this converts any url to lang specific, sample
/contact to /fr/contact
/about to /fr/about
update
for remove the lang part is simple
function removeLng() {
// remove language
var path = window.location.pathname.replace(/\/[a-z]{2}/, '');
window.location.href = path;
}

change URL using javascript Jquery

I'm trying to change the URL in the address bar using javascript.
So if the user access the page using
www.example.com/ajax/project8.html
Url should be changed automatically to
www.examp.com/#cbp=ajax/project8.html
shouldn't be any harder than this:
window.location = "http://whatever.you.want.com"
UPDATE
So you want your site to redirect to another page when the url is www.example.com/ajax/project.aspx?id=whatever and id=xxx could be any id.
To achieve that you need a function that returns the query string parameter value eg:id=whatever
Then check if the current url needs to be redirected to another page. If this is the case then redirect to new url with same parameter value.
/*
function that returns a query string parameter value
this function works with many parameters
Eg: www.example.com/#cbp=ajax/project.aspx?myParam=hello&id=1283&otherParam=234
to get the param value just give it the parameters name
getQueryStringValue("id") returns : 1283
getQueryStringValue("myParam") returns : "hello"
*/
function getQueryStringValue( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
//current url
var currentUrl = location.href;
//check if current url contains www.example.com/ajax/project.aspx
if (currentUrl.indexOf("www.example.com/ajax/project.aspx") != -1 ){
//new url for redirection
var newUrl = "www.example.com/#cbp=ajax/project.aspx?id=" + getQueryStringValue( "id" );
//redirect to new page
location.href = newUrl;
}
Try this code
if (window.location.href == 'www.example.com/ajax/project8.html') {
window.location = 'www.examp.com/#cbp=ajax/project8.html';
}
you can set all things like
window.location.href = "www.examp.com/#cbp=ajax/project8.html"
for more details how you will manage all url parameter then please see
JavaScript and jQuery url managment
window.location.href = "#cbp=ajax/project8.html";
you can change the value written after # to any location , div id etc.
e.g
window.location.href = "#myDivID";
<meta http-equiv="refresh" content="0; url=http://example.com/" />
Note: please put on header
or
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>

window.location.replace is not working

function setInit(){
iu.init({
saveFile : 'store/saveImageFile'
,CallBackOnSave : function (s){
console.log("redirect");
window.location.replace("store/details");
}
});
}
iu is an image upload module which has no issues. In fact I can see the console log of redirect.
problem is, the same page gets reloaded, instead of going to the url in replace.
tried other ways like href as well, sadly the same result.
Just use:
window.location.href = "store/details";
Maybe try:
parent.window.location.href = "store/details";
You can use by this way,
Syntax
window.location.href = window.location.host + relativePath
window.location.href = relativePath
example
window.location.href = "store/details";
window.location.href = window.location.host + 'store/details';

How do I check href attribute and if needed add full url address?

I want to check the href attribute and if it does not contain the full path, I want to replace it with full URL? Should I use JavaScript which does that but don't work on IE 8?
My JS:
fullUrl = $(this).filter('[href^='+document.location.protocol+']').attr('href')
? true : false;
url = fullUrl ?
$(this).filter('[href^='+document.location.protocol+']').attr('href')
: document.location.protocol + '//' + document.domain + $(this).attr('href');
I need to check if href contain full url or not:
href: "/path/other.html" (part)
href:"http://domain.com/path/other.html" (full url)
And then if i have part url href, i must add domain and recreate href to full url!
The full url is available via the .href property.
Typically there's no need to set it to the attribute, but if you want to, you could do this:
$('a[href]').attr('href', function() { return this.href; });
This finds all <a> elements that have an href attribute, and updates its href using the attr()[docs] method by passing a function as the second parameter, which returns the value of the .href property.
If you just wanted a list of them, you can use the map()[docs] method.
var hrefs = $('a[href]').map(function() { return this.href; }).get();
Example: http://jsfiddle.net/EDhhD/
EDIT:
If you want to explicitly check that the path isn't already the full path, just add an if statement to my first example.
$('a[href]').attr('href', function(i,hrf) {
if( hrf.indexOf( 'http' ) !== 0 ) return this.href;
});
I guess you want this:
$(this).attr('href', function(i, v) {
if ( v.indexOf('http:') === -1 ) {
v = document.location.protocol + '//' + document.domain + '/' + v;
}
return v;
});
Live demo: http://jsfiddle.net/simevidas/bwmQ5/
I don't think you can get a meaningful result of calling attr on multiple items. You should do your check/replacement in a loop, one item at a time:
$(this).filter('[href^='+document.location.protocol+']').each(function(){
var fullUrl = $(this).attr('href') ? true : false;
var url = fullUrl ? $(this).attr('href') : document.location.protocol + '//' + document.domain + $(this).attr('href');
alert(url);
});
This solution will ensure all anchors on your page have the protocol in the href:
$(document).ready(function()
{
var str_len = document.location.protocol.length;
$('a').each(function()
{
if($(this).attr('href').substring(0,str_len) != document.location.protocol)
{
$(this).attr('href',document.location.protocol + '//' + $(this).attr('href'));
}
});
});
http://jsfiddle.net/3XQXL/

Categories

Resources