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.
Related
I create a function using Javascript for related post on my Blogger template,
Here is my code:
function toHttps(link) {
var protocol=link.replace(/\:/g,'');
if(protocol=='http') {
var url=link.replace('http','https');
return link.replace(url);
}
}
if my original url is
https://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html
Why is the result like this?
https://dpawoncatering.blogspot.com/2008/08/undefined?
Naren Murali's answer is correct. I'd just like to add a different way of doing "protocol" swap using javascript's own URL parser that might be interesting for other people.
You can instantiate an a element and use its href attribute to parse your URL, then you can access and change the protocol attribute of the href and retrieve the resulting URL:
function toHttps(link) {
var url = document.createElement('a');
url.href = link;
url.protocol = 'https';
return url.href;
}
Since the URL contains https already it does not enter the if condition, hence nothing is returned hence we get undefined, please check my corrected function. Let me know if you have any issues!
function toHttps(link) {
if(link.indexOf('http://') > -1){
var url=link.replace('http','https');
return url;
}
return link
}
console.log(toHttps('http://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html'))
console.log(toHttps('https://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html'))
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.
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]
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 #.
I'm trying to set up a redirector so that when my AJAX functions change the hash part of the URI, the link is still directly accessible should it be copied and pasted. My current function is below; however, it always returns false!
//If a hash is found, redirect it
var current_uri = String(window.location);
if (current_uri.search('/\#/') != -1) {
var current_uri_array = current_uri.split('#');
window.location = current_uri[1];
}
How can I change the code to make this work? Is there a better way of doing this?
Code updated to:
if (window.location.hash) {
window.location = window.location.hash.substring(1);
}
Which worked.
Try using window.location.hash directly ;)