How can i retrieve variables from the URL address?
am using this code but it is not working:
var x=window.location;
var y = x.indexOf('#', 0);
x=x.substring(y,x.length);
am receiving the following error:
Error: x.indexOf is not a function
What you want is not window.location but window.location.href
Still, I would use window.location.hash
You get the error because window.location is an object, not a string. Try window.location.hash to obtain the part after the #.
Related
I have a JavaScript program that worked until I tried to change this: "foldername" to this: http://hokuco.com/test/"+"foldername"+"/index.html".
what is wrong with my code?
For anyone interested entire JS:
document.getElementById("submit").addEventListener("click", function(){
var url = document.getElementById("http://hokuco.com/test/"+"foldername"+"/index.html").value;
window.location.href = "url";
});
<input type id="foldername"></input>
<input type ="button" id ="submit/>
You probably meant:
document.getElementById("submit").addEventListener("click", function(){
var url = "http://hokuco.com/test/" + document.getElementById("foldername").value + "/index.html";
window.location.href = url;
});
Changes:
The parameter in the getElementById function is the same as the id attribute on the input element with the id "foldername".
The window.location.href should be set to a variable, not a quoted string.
More legibly, you would want:
document.getElementById("submit").addEventListener("click", function(){
var folder = document.getElementById("foldername").value;
var url = "http://hokuco.com/test/" + folder + "/index.html";
window.location.href = url;
});
Now, hopefully, it is much more clear about what's going on.
Please read the following documentation to better understand document.getElementById: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
Your code will most likely return an uncaught type error when trying to access a property of null. You must only pass a string referring to an Element object's ID.
What are you trying to accomplish? It looks like you are trying to redirect but are using a string literal instead of a variable.
I have a variable but I can't use it the way I want, please help!!
var test = window.location.hash;
$('div').load("test.php?id="+test);
The request keeps on being :
XHR finished loading: "http://localhost/test-site/test.php?id=".
and ignores my variable...
window.location.hash will begin with a # symbol, if it contains anything at all. You should strip it by adding .substr(1):
var test = window.location.hash.substr(1);
$('div').load("test.php?id="+test);
As it is, you are trying to load a url like test.php?id=#22, and since the hash is meaningless for AJAX purposes, it's being ignored by the .load method.
var test = window.location.hash.substring(1);
How can I return the hash value of website.com/#something (something) from the URL with jQuery?
window.location.hash
its that simple.
donot use all those methods which consume CPU and effects performance.
If DOM provides something predefined use it first.
To pass value to PHP please do and ajax call to php.
var hash = window.location.hash;
$.ajax({
url: 'someurl.php',
data: {hash: hash},
success: function(){}
})
You can use the location.hash property to grab the hash of the current page:
var hash = window.location.hash;
update
As there is a built in method to get the hash via DOM above answer is not appropriate
var hashTag = window.location.hash
alert(hashTag);
will do the magic.
Old answer
You can do something as below if you have multiple hashes in your url
//var href = location.href; // get the url in real worl scenario
var href = "www.bla.com#myhashtag"; // example url
var split = href.split("#"); // split the string; usually there'll be only one # in an url so there'll be only two parts after the splitting
var afterSplit = "Error parsing url";
if(split[1] != null){
afterSplit = split[1];
}
// If everything went well shows split[1], if not then de default error message is shown
alert(afterSplit);
Here is an example Live Fiddle
You could use this
h=new URL(location).hash.split`&`.find(e=>/hash_name/.test(e)).split`=`[1]
I have a sample url website: http://mysite.com/
var host = window.location.protocol+"//"+window.location.hostname;
$.ajax({
type:"POST",
data: params,
url : host+'/forms/get_data.php',
success:function(data){
...othercodeblahblah
}
});
Why is it that when I try to check my firebug it makes the URL weird.
This is the sample output of firebug:
http://mysite.com/mysite.com/forms/get_data.php
With this url it now gives me:
"NetworkError: 404 Not Found - http://mysite.com/mysite.com/forms/get_data.php"
Shouldn't it output like http://mysite.com/forms/get_data.php ?
Why is it giving me a wrong url path?
Your help would be greatly appreciated and rewarded!
Thank!
The reason is window.location.protocol already includes a colon (:).
The host variable therefor contains http:://mysite.com
jQuery picks up that you didn't pass a full valid URL, so it prepends your hostname automatically.
The fix is changing
var host = window.location.protocol+"://"+window.location.hostname;
to
var host = window.location.protocol+"//"+window.location.hostname;
Edit
I created a jsfiddle with your code: http://jsfiddle.net/xH5ZV/
and the fixed code: http://jsfiddle.net/xH5ZV/1/
Notice in the fixed code you don't get the hostname twice.
I'm not sure where such an error might come from, but specifying the host is redundant: AJAX requests are same-domain anyway (unless specifically configured), so just specify a part from the root:
url: "/forms/get_data.php",
I have code:
function _filter() {
var url = window.location;
alert(url);
alert(url.split("/")[1]);
}
When I launch it I get only one alert message:
http://localhost:8000/index/3/1.
Why I don't get the second alert message?
Adding .toString() works and avoids this error:
TypeError: url.split is not a function
function _filter() {
var url = window.location;
alert(url);
alert(url.toString().split("/")[2]);
}
When run on this very page, the output is:
stackoverflow.com
The location object is the cause of this, window.location is an object not a string it is the location.href or location.toString().
function _filter() {
var url = window.location.href; // or window.location.toString()
alert(url);
alert(url.split("/")[1]);
}
The value of window.location is not a string, you want the href property of the location object:
function _filter() {
var url = window.location.href;
alert(url);
alert(url.split("/")[1]);
}
Because your url is ans object so you need to convert this to string than you apply split function
function _filter() {
var url = window.location+ '';
alert(url);
alert(url.split("/")[2]);
}
The index [1] is in between the two slashes of http:// which is null and wont be alerted. Index [2] is the localhost:8000 you're probably looking for.
Simple window.location.hostname should be useful too.
To understand how many pieces you get from splitting operation you can alert the .lenght of url.split, are you sure that the script doesn't block?
Use firebug to understand that
url.split("/")[1] will equal to null. So, it alert(null) will not display msg.