Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 715
Jay M Avatar asked Sep 24 '26 11:09

Jay M


2 Answers

shouldn't be any harder than this:

window.location = "http://whatever.you.want.com"
like image 184
adrichman Avatar answered Sep 25 '26 23:09

adrichman


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;


        }
like image 20
Merlin Avatar answered Sep 26 '26 00:09

Merlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!