0

Sometimes because of cache I add # in my url, like this:

http://www.example.com/#lang=3201954253

What I need is to check if there is #lang in url and remove it if present.

4 Answers 4

2

you can clear the hash.

window.location.hash = '';

or you can even use history api History Api. history.pushState and replaceState

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.

window.history.replaceState( {} , 'foo', '/foo' );
Sign up to request clarification or add additional context in comments.

Comments

1

You may try like this:

if(window.location.hash) {
  // code
} else {
  // code
}

or you may try this:

<script type="text/javascript">
    if (location.href.indexOf("#") != -1) {
        //code
    }
</script>

If you want to remove it then you may try this:

window.location.hash = ''

On a side note:

You may try

window.location.href.split('#')[0]

to remove anything after # without refreshing your page.

3 Comments

@Schneider window.location.hash = '' will do that for you
@Schneider:- You may also try window.location.href.split('#')[0] to remove the anything after # without refreshing your page
@Schneider:- You may also use this fucntion: function removeHash() { window.history.pushState("", document.title, window.location.pathname); btn.disabled = !(location.hash || location.href.slice(-1) == "#"); }
0
if (window.location.hash.indexOf("lang")) {
  window.location.hash = "";
}

Comments

0
var tel = window.location.href;

if(tel.indexOf("#") > -1){
    alert("found");
} else {
    alert('not found')   
}

1 Comment

Example is just example i dont know the url :(( Also i want to remove it :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.