How do I read #2 at the end of a URL? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you check for a #hash in a URL using JavaScript?
Currently I am generating Urls that look like this:
InspectionPhotos.aspx?inspectionId=10001649#/2
The #2 is for a photogallery plugin, and this would mean go the second photo.
I would like to show a div only if there is a #/[anynumber] but if its just
InspectionPhotos.aspx?inspectionId=10001649
then not show anything.
How could I do this check? Either asp.net on pageload or a client side javascript would be fine.
Thanks.

You can't do this in server side, because the hash is not sent to the server, to get this value with javascript is simple:
var hash = window.location.hash;
if (hash){
//use the hash value.
}

JavaScript: window.location.hash will comeback with #/2 from your example.

Related

How to check for a specific word in the URL before running script? [duplicate]

This question already has answers here:
How to make a script redirect only once every time an appropriate page loads?
(4 answers)
Closed 3 years ago.
I have the current script to amend a link in the URL bar
(function() {
'use strict';
location.replace(location.href.replace("www.reddit.com", "old.reddit.com"));
})();
I'd want it to check if the URL has the word "old" before running the remainder of the script. How do I do this?
You can check the index of the word old by considering the URL as a normal string.
if(location.href.indexOf('old') > -1) {
// do something
}
It is simple as that.

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

PHP $_Get and JQuery .load [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I want to load another page via Javascript like this
$("#resultarea").load("searchresults.php?searchstring=" + $("#searchbox").val());
in searchresults.php, a single statement
echo $_GET["searchstring"];
what I type in searchbox appears when searchresults.php is loaded except when I add space and another word, no thing appear at all. I heared it can be done by encoding or somewhat, I searched but didn't find a solution.
Try encodeURIComponent:
var encodedValue = encodeURIComponent($("#searchbox").val());
$("#resultarea").load("searchresults.php?searchstring=" + encodedValue);
Source
Demo

javascript get querystring [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
obtaining querystring from current url in javascript?
I was to get everything after the question mark in the url. Ive seen a way to do it that does not require a function but cannot find it for the life of me.
url
fsdhfbsdfj/index.html?hello
get
hello
Use
var query = window.location.search;
If you want to get rid of the starting "?", use
var query = window.location.search.slice(1);
The properties of the location object are described here.
var querystring=window.location.search.substring(1);
Your title says "get querystring", but your question says "get everything after the question mark" which is something different and can include both a querystring and a hash.
I assume that when you say "the" url you are talking about the current Web page.
To get the querystring:
window.location.search
To get everything after the first ?:
window.location.href.split("?").slice(1).join("?");
You can find the query string in window.location.search

Capture the removal of a hash [duplicate]

This question already has answers here:
How can I detect changes in location hash?
(11 answers)
Closed 8 years ago.
I have attached certain actions to the existence of a URL hash in order to mimic traditional 'back' behavior.
The trouble is, if you go back from the hash, the hash is removed from the URL string, but the JavaScript doesn't pick up that that hash has gone.
How can I make it pick up the removal of the hash?
Usually clicks go first, then location changes.
After a click is a good idea to set TimeOut
to get an updated window.location.hash as shown below.
$(".nav").click(function(){
setTimeOut(function(){
updatedHash = location.hash
},100);
});
or you can listen for the location with:
window.onhashchange = function(evt){
updatedHash = "#" + evt.newURL.split("#")[1]
};
I wrote a jQuery plugin that does something like what you want to do. https://github.com/rgarro/Route32 It's a simple anchor router.
Poll window.location.href to detect changes. Use the change to work out what needs to be reverted. see How can I detect an address bar change with JavaScript?
check the window.location.hash string, it will return the hash part of your url

Categories

Resources