2

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

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
Jay M
  • 103
  • 1
  • 1
  • 8
  • possible duplicate of [How can I make a redirect page in jQuery/JavaScript?](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript) – MackieeE Apr 14 '14 at 10:01

6 Answers6

5

shouldn't be any harder than this:

window.location = "http://whatever.you.want.com"
adrichman
  • 1,215
  • 10
  • 17
4

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;


        }
Merlin
  • 4,907
  • 2
  • 33
  • 51
  • project8.html is a page from bunch of other pages that i want to change the url for, sorry for the inconvenience but think of it as project.aspx?id=8 – Jay M Apr 14 '14 at 05:17
  • Sorry I don't understand what you want.. ? Explain your problem more precisely please.. You want to change the url for many pages ? – Merlin Apr 14 '14 at 05:19
  • yes, i want to redirect user from www.example.com/ajax/project.aspx?id=xxx to www. example.com/#cbp=ajax/project.aspx?id=xxx – Jay M Apr 14 '14 at 05:29
2

Try this code

if (window.location.href == 'www.example.com/ajax/project8.html') {
  window.location = 'www.examp.com/#cbp=ajax/project8.html';
}
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
1

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

Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
1

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";

vsnahar
  • 77
  • 3
0
<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>
airi
  • 585
  • 5
  • 21