Jay M
Jay M

Reputation: 103

change URL using javascript Jquery

I'm trying to change the URL in the address bar using javascript. So if the user access the page using

www.example.com/ajax/project8.html

Url should be changed automatically to

www.examp.com/#cbp=ajax/project8.html

Upvotes: 2

Views: 29052

Answers (6)

vsnahar
vsnahar

Reputation: 77

window.location.href = "#cbp=ajax/project8.html";

you can change the value written after # to any location , div id etc. e.g window.location.href = "#myDivID";

Upvotes: 1

Merlin
Merlin

Reputation: 4917

UPDATE

So you want your site to redirect to another page when the url is www.example.com/ajax/project.aspx?id=whatever and id=xxx could be any id.

To achieve that you need a function that returns the query string parameter value eg:id=whatever

Then check if the current url needs to be redirected to another page. If this is the case then redirect to new url with same parameter value.

        /*
        function that returns a query string parameter value
        this function works with many parameters
        Eg: www.example.com/#cbp=ajax/project.aspx?myParam=hello&id=1283&otherParam=234
        to get the param value just give it the parameters name
        getQueryStringValue("id") returns : 1283
        getQueryStringValue("myParam") returns : "hello"
        */
        function getQueryStringValue( name ){
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]" + name + "=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( window.location.href );
          if( results == null )
            return "";
          else
            return results[1];
        }

        //current url 
        var currentUrl = location.href;

        //check if current url contains www.example.com/ajax/project.aspx
        if (currentUrl.indexOf("www.example.com/ajax/project.aspx") != -1 ){

            //new url for redirection
            var newUrl = "www.example.com/#cbp=ajax/project.aspx?id=" + getQueryStringValue( "id" );

            //redirect to new page
            location.href = newUrl;


        }

Upvotes: 4

Rituraj ratan
Rituraj ratan

Reputation: 10378

you can set all things like

window.location.href = "www.examp.com/#cbp=ajax/project8.html"

for more details how you will manage all url parameter then please see

JavaScript and jQuery url managment

Upvotes: 1

Sachin Jain
Sachin Jain

Reputation: 21842

Try this code

if (window.location.href == 'www.example.com/ajax/project8.html') {
  window.location = 'www.examp.com/#cbp=ajax/project8.html';
}

Upvotes: 2

airi
airi

Reputation: 585

<meta http-equiv="refresh" content="0; url=http://example.com/" />

Note: please put on header

or

<script type="text/javascript">
 window.location.assign("http://www.example.com")
</script>

Upvotes: 0

adrichman
adrichman

Reputation: 1235

shouldn't be any harder than this:

window.location = "http://whatever.you.want.com"

Upvotes: 5

Related Questions