Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  25] [ 5]  / answers: 1 / hits: 23943  / 12 Years ago, sun, november 18, 2012, 12:00:00

I would like to change the URL that appears once a form has been submitted although the code below does not appear to do what I would like it to do. How do I change the URL in the address bar?



CODE



<!DOCTYPE html PUBLIC -//W3C//DTD XMHTML 1.0 Strict//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>

<html xmlns=http://www.w3.org/1999/xhtml xml:lang=en lang=en>
<head>
<meta http-equiv=content-type content=text/html;charset=UTF-8 />
<meta http-equiv=content-language content=en-us />
<meta http-equiv=cache-control content=no-cache />
<meta http-equiv=pragma content=no-cache />
<meta name=keywords content= />
<meta name=description content= />
<meta name=author content= />
<meta name=copyright content=&copy; 2012 />
<meta name=robots content=noindex, nofollow />

<title>sample form</title>

<link rel=stylesheet type=text/css media=all href= />

<style type=text/css>

</style>

<script type=text/javascript>

var testurl = window.location.href;
if(testurl == file:///C:/Users/user/Desktop/sample_form.html?q=test&submit=submit) {
testurl = testurl.replace(/q=[^&]+&[a-z]+=[a-z]+/,'test');
alert(testurl);
}

</script>
</head>

<body>

<form name=sampleform id=sampleform method=get action=sample_form.html>
<input type=text name=q value= id=q />
<input type=submit name=submit value=submit id=submit />
</form>

</body>
</html>

More From » javascript

 Answers
39

Try the following:



<script type=text/javascript>

var testurl = document.URL;
if(testurl == file:///C:/Users/user/Desktop/sample_form.html?q=test&submit=submit) {
testurl = testurl.replace(/q=[^&]+&[a-z]+=[a-z]+/,'test');
window.location.href = testurl;
}

</script>


A note:




Use window.location for read and write access to the location object
associated with the current frame. If you just want to get the address
as a read-only string, you may use document.URL, which should contain
the same value as window.location.href



[#81925] Friday, November 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adrienkeithr

Total Points: 503
Total Questions: 126
Total Answers: 110

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
;