Javascript / jQuery - Strip the current page URL
I am currently working on an Enquiry system for our travel website.
When a user submits an enquiry, i have set it to reload the same page (by grabbing the current url and setting it as the redirect).
Upon redirecting my javascript detects if the following string is present in the URL - "?sendto", and if so it returns a thank you message.
After this process, im looking to remove the "?sendto=email@address.com" string from the end of the url (example url - www.google.com/index.html?sendto=email@address.com). My question is therefore:
Am i able to through a script, strip a part of the current URL开发者_JAVA技巧
I'd suggest looking into bbq jquery that who library seems to be what you're looking for. On a simple note 'google.com?email=...".split('?')
As Scoop says I'm not sure if this is possible. I had a similar scenario where I was passing a QueryString back to the server to draw a chart. I didn't want this to appear in the address bar so I stripped it out on the server side after getting all the useful info from it.
This could be done very easily with something like Jquery Ajax, Xajax, etc. You simply display the form, onclick submit via ajax to the server, and on success inner html the div with the form to be your thankyou message. No need to edit the url string at all. If you feel the need to know the process happened, set a session variable.
You frequenly also see this done server side in basic PHP tuts, where there's a case statement on a form page such as:
if ($_POST) {
//process form
//echo message
} else {
//echo form here
}
Then the form submits to itself and displays the message. Not ajax-y, but it's been done that way for years with success.
If you're asking whether you can change the displayed URL in the browser bar, the answer is no. The reason? Would you want some random site to be able to change their URL to http://bankofamerica.com
?
If you're asking how you can strip it off conveniently for the user (and are ok with a redirect), do something like this:
document.location = document.location.toString().replace(/\?.*/, "")
which will change a url like
http://google.com/index.html?sendto=email@address.com
to
http://google.com/index.html
but it will do it by redirecting the user's browser to the new url.
Try this: http://snipplr.com/view/12659/parseurl/
You can use it to parse your url into individual components. Something like the following:
var url = parseURL(window.location.href); window.location = url.protocol + '://' + url.host + url.path;
精彩评论