Capture the removal of a hash [duplicate] - javascript

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

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.

Create a script to create a link which is a modified version of the current URL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a site with sequential page naming.
Take the below as an example
mysite.com/index.html
mysite.com/book-1.html
mysite.com/book-2.html
I want to create a button that runs a script to get the current URL and increment the number after "book-" by 1. This would effectively be used as a "Next Page" button. I want to avoid using hard coded links because the real site has over 900 pages. This would be useful since I would simply need to copy a single snippet of code to on to each page as opposed to taking the time to manually change each and every link.
To do this i plan on using JS to:
get the URL of the current web page
split up the text string by the "-" character
increment the number after the "-" character by 1
concatenate the term "www.mysite.com/book-" and the newly evaluated number from step 3
I then want to use this resulting concatenated text string as the value for the href attribute of an HTML tag.... it should look something like the following:
<a href="*concatenated string from step 4*" >
What would be the simplest javascript code to use for steps 1-4 and what is the correct syntax to be used inside the tag so that the hyperlink would point to the URL generated in step 4 (the concatenated string)?
My answer basically gets the URL, splits it, then gets the number of the book on the current URL then adds 1 to it, then sets the href attribute to the next book...
Although, I clearly don'y have a URL you your website, So where the comment is, replace the string with document.URL... This has a vault of the current URL (which obviously isn't your website.)
var URL = "http://example.com/book3.html"; // Replace this with document.URL in your actual script
var URL_split = URL.split("/");
var URL_return_num = parseInt(URL_split[URL_split.length - 1].replace(/book(.*?)\.html/i, "$1"));
document.getElementById("next_book").setAttribute("href", "book"+(URL_return_num+1)+".html");
I will run through what this actually does.
var URL: this stores the URL for future use in the script.
var URL_split: This changes the URL into an array, for each "/" in the text.
var URL_return_num: This is a quite compact one. This basically fetches the last part of the split URL which is "book3.html"... It removes the "book" and the ".html" with regex. Then it turns it into an int, so it can be added to.
"book"+(URL_return_num+1)+".html": This creates a string, basically saying "book" + (current_book_number + 1) + ".html", in this case (on the page), it would be "book" + (3+1) + ".html"
Here is a JSFiddle example, although it's not much use because it's not on your website.
http://jsfiddle.net/XeroElixir/Uwg7n/1/

Show more than 10,000 characters in a JavaScript Alert [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript object max size limit at 10000 chars
I want to check whether html source contains a specified string, but I'm only getting 10,000 characters (in alert box)
var str=document.documentElement.innerHTML;
if(str.indexOf("abcxyz") !== -1)
{
alert(str);
}
How can I fix that?
If you need to display a huge string in alert boxes, I guess you could split it into 10,000-character chunks and display them in order. Alternatively, just use console.log to print it out.
And is there a better way to detect a "onClick" event?
Better than what? JavaScript events will bubble up to the top containing element (unless some element in the chain calls stopPropagation()), so an easy way to detect click events would be to attach a click handler to document.body:
document.body.onclick = function() { alert('Click!'); }

JS: Append variable to URL without repeating [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change URL parameters with jQuery?
Currently, I have a <div> tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes or &view_all=No to the URL.
<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>
Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes or &view_all=No to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.
I have figured out a way to do the append but it keeps adding the &view_all variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No.
This is the way I am doing the append:
onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"
You can use regular expression to replace the value in a string:
$(this).attr("onclick", function(i, val) {
return val.replace(/view-all=(Yes|No)/, function() {
return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
});
});

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

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.

Categories

Resources