473,700 Members | 2,616 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 1856

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
8158
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 do this on the server side in the SQL area of the ASP page by writing a function (with some great...
0
1354
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
2887
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
56180
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
29626
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
2793
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 cell in yellow. After scanning and coloring all of my inventory I can then see exactly what is...
5
1836
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
1187
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
8729
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9217
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7815
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6563
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5903
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4407
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2031
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.