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

1

6 Answers 6

5

shouldn't be any harder than this:

window.location = "http://whatever.you.want.com"
Sign up to request clarification or add additional context in comments.

1 Comment

i think the challenge is todo this without page refresh
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;


        }

3 Comments

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
Sorry I don't understand what you want.. ? Explain your problem more precisely please.. You want to change the url for many pages ?
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
2

Try this code

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

Comments

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

Comments

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

1 Comment

super like to this answer. I want exactly this
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.