Schneider
Schneider

Reputation: 2496

How to check in # in url?

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.

Upvotes: 0

Views: 91

Answers (4)

Sudharsan S
Sudharsan S

Reputation: 15393

var tel = window.location.href;

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

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

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.

Upvotes: 1

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

if (window.location.hash.indexOf("lang")) {
  window.location.hash = "";
}

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15861

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' );

Upvotes: 2

Related Questions