Add a fragment to the URL without causing a redirect?

JavascriptJqueryHash

Javascript Problem Overview


Is there is a way how to add hash # to my URL without redirect?

Javascript Solutions


Solution 1 - Javascript

window.location.hash = 'something';

That is just plain JavaScript.

Your comment...

> Hi, what I really need is to add only the hash... something like this: window.location.hash = '#'; but in this way nothing is added.

Try this...

window.location = '#';

Also, don't forget about the window.location.replace() method.

Solution 2 - Javascript

For straight HTML, with no JavaScript required:

<a href="#something">Add '#something' to URL</a>

Or, to take your question more literally, to just add '#' to the URL:

<a href="#">Add '#' to URL</a>

Solution 3 - Javascript

window.location.hash = 'whatever';

Solution 4 - Javascript

Try this

var URL = "scratch.mit.edu/projects";
var mainURL = window.location.pathname;

if (mainURL == URL) {
	mainURL += ( mainURL.match( /[\?]/g ) ? '&' : '#' ) + '_bypasssharerestrictions_';
	console.log(mainURL)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeeView Question on Stackoverflow
Solution 1 - JavascriptalexView Answer on Stackoverflow
Solution 2 - JavascriptJess TelfordView Answer on Stackoverflow
Solution 3 - JavascriptDaniel Lo NigroView Answer on Stackoverflow
Solution 4 - JavascriptTladi RamahataView Answer on Stackoverflow