473,563 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript's "Replace" Problem

I have a problem using the REPLACE method. Here is a short description:

In a.html:

I used the following to call the chgtitle function:
<a href=" " onClick="chgtit le('b.html?myti tle=old_title') ">Old Title</a>

function chgtitle(name) {
window.location .replace(name);
//alert(name);
}

The result is my browser (Firefox) ends up reloads a.html again after
loading b.html You can't even see b.html. But if I un-comment
alert(name), it seems to stop going further and it stops at b.html. But
this is not the proper way to do this. It seems I am missing something here.

(The reason I need this is I do not want user to be able to go back
after changing something.)

Please help and comment.
Mar 16 '06 #1
3 1849

Louis wrote:
I have a problem using the REPLACE method. Here is a short description:

In a.html:

I used the following to call the chgtitle function:
<a href=" " onClick="chgtit le('b.html?myti tle=old_title') ">Old Title</a>

function chgtitle(name) {
window.location .replace(name);
//alert(name);
}

The result is my browser (Firefox) ends up reloads a.html again after
loading b.html You can't even see b.html. But if I un-comment
alert(name), it seems to stop going further and it stops at b.html. But
this is not the proper way to do this. It seems I am missing something here.

(The reason I need this is I do not want user to be able to go back
after changing something.)

Please help and comment.


There is no problem with the replace() method. The actual problem is
you failed to stop the default action happening when you click on the
link. Therefore, you should actually do the following:

javascript:

function chgtitle(name)
{
location.replac e(name);

return false;
}

html:

<a href = "path/nojavascript.ht ml" onclick = "return
chgtitle('etc.' )">etc, etc.</a>

It is recommended that you actually provide an URI on links for
graceful degradation.

Mar 16 '06 #2
Louis wrote:
I have a problem using the REPLACE method. Here is a short description:

In a.html:

I used the following to call the chgtitle function:
<a href=" " onClick="chgtit le('b.html?myti tle=old_title') ">Old Title</a>

function chgtitle(name) {
window.location .replace(name);
//alert(name);
}

The result is my browser (Firefox) ends up reloads a.html again after
loading b.html You can't even see b.html. [...]


You need to cancel the event

<a href=" " onclick="...; return false;">...</a>

Apparently, the `href' attribute value containing only whitespace is
normalized to an empty attribute value which would be a URI reference
for the referring document resource (a.html) itself, or the beginning
of that document. See RFC3986.

You should not depend on normalization (use an empty attribute value
instead), and you should feature-test window.location .replace() before
you call it. See the FAQ and FAQ Notes for details.
PointedEars
Mar 16 '06 #3
web.dev wrote:
Louis wrote:
<a href=" " onClick="chgtit le('b.html?myti tle=old_title') ">Old Title</a> ^ [...]

[...]
It is recommended that you actually provide an URI on links for
graceful degradation.


Unless an appropriate action is desired. With an empty URI _reference_, it
is desired, as that will reload the current document. However, one space
character is not an URI reference, so the value should be replaced by the
empty string in that case.
PointedEars
Mar 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
8141
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so that the apostrophe is automatically replace with two '' . Reason being--SQL Server does not like apostrophes being sent to database. I've tried to...
0
1346
by: Rick Brandt | last post by:
Here is a simplified example of what I am passing to a Java SAX parser within a servlet via an HTTPRequest... <?xml version="1.0"?> <Record> <ID>1092853685</ID> <Notes><!]></Notes> </Record> I get the following error in the server's log output...
1
2880
by: Luke Dalessandro | last post by:
I have an application where there is a primary XML data file. I'll use the following as an example: <data> <item id="a"> <name>A</name> <price>$10</price> </item> <item id="b"> <name>B</name>
6
56057
by: vigi98 | last post by:
Hello all, Can someone confirme that this: var strURLpiece = "UK & Ireland"; strURLpiece.replace("&", "%26"); replaces all occurrence of the character & by %26 in strURLpiece, ie that strURLpiece becomes "UK %26 Ireland" ? Thanks a lot.
6
29610
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or strMyString.Replace(System.Environment.NewLine, '_');
0
2788
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that...
5
1829
by: Curious | last post by:
I have: if (temp.Contains("Account") == true) { temp.Replace("Account", "Client"); } The "Account" is not replaced by "Client" after this operation. I used
1
1185
by: deepthi230 | last post by:
i am using the method parseFromString() to parse a string into DOM tree. i want to replace \" in the string with " before it is passed to the method, using javascript. how can i do this??
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.