user2631318
user2631318

Reputation:

How do I change URL without reloading page? - WITH A TWIST

I am looking to remove a certain amount of a page URL without reloading.

I have looked on StackOverflow for the answer and there are a few close answers but not what I'm looking for really.

I do not want to use the hash function and if possible, avoid using the HTML5 version because HTML5 isn't the ratified standard yet and for the website this will be used on, I wouldn't be at all surprised is some of the clients used IE6.

I would prefer if it wasn't an extremely complicated solution with 400 lines of code (at that cost I'd rather just reload the page to be honest).

I forgot to mention this, critical, part of the question

The user is directed from a different page with this string via PHP. So for example:

user submits a form with errors. Let's say the form is on /myform.php

The user gets headed to /index.php?string

carry on as normal

What I have:

This is an example URL that I wish to edit without reloading:

http://www.mywebsite.com/index.php?string

Wait! ?string has a use on /index.php

index.php will use $_GET['string']; and display a message to the user

I have used JQuery to remove the message after 4 seconds like this:

<div id="pagemsg"><?php echo $string; ?></div>

<script language="javascript">
    setTimeout( "$('#pagemsg').fadeOut();", 4000);
</script>

This works all well and the message disappears after 4 seconds. However I'm left with this URL:

http://www.mywebsite.com/index.php?string

I don't want this string anymore because ?string has done its job!

I would like to remove the ?string without reloading the while page.

Is there any solution that can help me?

Also,

I don't mind if I have to use the HTML5 push function but is there a fall-back solution for the browsers that don;t support HTML5?

Why did I put WITH A TWIST in the title? Well, my requirements are different to the questions I have seen so far. Correct me if I'm wrong and I will change it.

Thanks for your help in advance!

Upvotes: 1

Views: 217

Answers (2)

Jonathan Amend
Jonathan Amend

Reputation: 12815

The simple answer is no. That's why they added pushState.

However, it looks like you are trying to accomplish a fairly common scenario known as flash messaging. This is often done using the user's session to store a message to show on the next request (and then delete it).

Example:

function doImportStuff() {
    // ...
    $_SESSION['message'] = 'important stuff was done';
    // ...
}

function showBoringPage() {
    // ...
    if (isset($_SESSION['message']) {
        echo '<b>' . $_SESSION['message'] . '</b>';
        unset($_SESSION['message']);
    }
    // ...
}

Upvotes: 1

DividedByZero
DividedByZero

Reputation: 4391

The only method I know of is this:

if (history && history.pushState){
   history.pushState(null, null, '/new/url');
}

But this is HTML5 and won't work in your case..
For old browsers - and you: History.js

Upvotes: 0

Related Questions