When using a url like this:
http://localhost/nafham/?selection/12/24/122
The hashing is done in that manner:
http://localhost/nafham/?selection/12/24/122#?selection/12/24/122/الصف-الثالث-الثانوي/السنة-كاملة/الاقتصاد/self
However when any other part added to the URL, example:
http://localhost/nafham/?selection/12/24/122/test
The hash is added again on each action
http://localhost/nafham/?selection/12/24/122/test#?selection/12/24/122/الصف-الثالث-الثانوي/السنة-كاملة/الاقتصاد/self
http://localhost/nafham/?selection/12/24/122//test#?selection/12/24/122/test#?selection/12/24/93/الصف-الثالث-الثانوي/السنة-كاملة/الاقتصاد/self
Any idea why the Hash is added to the URL instead of replacing the current hash value?
Because you are using the part of the URL when you set the hash.
Use location.hash = "...". It should work.
Related
Generally, We are using querystring to get the detail from the url. I am using the url like:
"example.com?q=abc"
in php with javascript. I need to use the url like
"example.com/abc"
Is it possible to use url like this "example.com/abc" ?
By definition, the QueryString is what is after the first question mark, see https://en.wikipedia.org/wiki/Query_string for example.
So your second url does not contain a QueryString. But it does not mean you cannot get the "abc" value with JavaScript: you can get the full url via
window.location.href
and then you can parse it.
I have fetched window.location into a variable. Now, I want to remove part of the string from the href within the location object, but still retain the object.
In other words, I want to modify the location object, and still be able to use window.location.protocol and window.location.host, but those functions need to work on the modified object.
For example, something like this where my browser displays "https://my.domain.org/site":
var thisloc = window.location;
Modify the href within that object to "http://my.domain.org/othersite"
//now I want it to fetch "http" instead of "https" based on my modified object
var thisprot = thisloc.protocol;
Will that work? If it does, it would be very nice. Otherwise, I have to parse the URL to get the protocol, host and pathname from a modified href, which would also accomplish the same goal.
Inspired by James Padosley's post on Parsing URLs with the DOM!, the trick here is to use an anchor element (<a>) as a URL builder/parser. With any anchor element, if you set its href property to some URL, it will be parsed by the DOM. Thereafter, you can freely modify the parts of that URL via properties on the anchor element. Query the href property at any time to see the full URL.
Using the example from the question…
var builder = document.createElement("a");
builder.href = "https://my.domain.org/site";
builder.protocol = "http://";
builder.pathname = "/othersite";
console.log(builder.href); // http://my.domain.org/othersite
At this point the anchor element's href will now be http://my.domain.org/othersite, as requested. Here's a JS Bin demonstrating it in action: http://jsbin.com/omoqen/1/edit.
The beauty is that anchor elements have the same URL component properties as the window.location object:
protocol
host
hostname
port
pathname
search
hash
These are browser properties - you can fetch and modify the values, but referring to the property won't work in the way you describe.
I'm looking for a way to retrieve the #anchor part from the current URL with JavaScript.
For example:
http://my-page.com/index.html#contact-us
Would return contact-us.
I could split the URI at the eventual # and then take the last piece, but I'm looking for a somewhat nicer and cleaner suggestion. A native (jQuery?) function would be great, but I guess I'm asking for too much.
Use location.hash:
location.hash.slice(1);
It starts with a #, hence .slice(1). Given an arbitrary string, you can use the built-in URL-parsing feature by creating a <a> element, and set the href, then read other properties, such as protocol, hostname, hash, etc. jQuery example:
$('a').attr('href', url)[0].hash;
location.hash.replace(/^#/, "")
If you work with a variable:
var url = "http://my-page.com/index.html#contact-us";
var hash = url.substring(url.indexOf("#") + 1);
I want to add hash to my URL. For example
http://somesite.com/somesubdomain#p1=1&p2=2&p3=3
when I try to do this, all I get is:
http://somesite.com/somesubdomain#p1=1%23p2=2%23p3=3
So, in short, how to I add special characters in my URL hash.
EDIT:
I am using YUI browser history manager.
var hash = "p1=1&p2=2&p3=3"
YAHOO.util.History.navigate("state",hash);
The way you're supposed to do this in YUI appears to be something like:
YAHOO.util.History.navigate('p1','1');
YAHOO.util.History.navigate('p2','2');
YAHOO.util.History.navigate('p3','3');
If you want the browser URL string to be http://somesite.com/somesubdomain#p1=1&p2=2&p3=3
The Calendar Example in the docs demonstrates this.
How would I go about getting the current URL using jquery, or more specifically, getting an ID on the end of it?
For example, I have product.php#tab-2. What I want to get from it is just the '#tab-2' part.
I have tried 'window.location.pathname' but that will only return '/product.php'
Thanks
You don't need jQuery for this:
alert(window.location.href); // will give you the full url
alert(window.location.hash); // will give you the hash (#) value
See the Mozilla docs at window.location - MDC.
You want window.location.hash
Use jqUrl plugin (http://www.oakcitygraphics.com/jquery/jqURL/jqURLdemo.html) to retrieve the full current url, then strip the window.location.pathname part.
Thomas
To actually assign to a variable, use the following;
var url = window.location.href;
var id = url.substring(url.lastIndexOf('#') + 1);
Note that if your url is in this format /product.php/3 , then you can use the above code, just change the character is the lastIndexOf function.