clicking on a link with the symbol "#" in the href attribute in Firefox prevents the setter

Good morning everyone,

It's early Monday morning and I'm having trouble understanding why this particular line works in Internet Explorer but not in Firefox.

<a class="button" href="#" onclick="setMaintenanceMode(false);">disable</a>

When you hover over the button in both IE and FF, the URL displayed is...

http://localhost:8080/mainapp/secure/gotoDevice.action?hardwareId=1&storeCode=2571#

Upon clicking the button, the following method is invoked...

function setMaintenanceMode(enabled) {
    var url = '<s:url action="secure/setMaintenanceMode"/>' + '&ModeEnabled=' + enabled;
    document.location.href = url;
}

The URL that the document is directed to is the same in both browsers...

/mainapp/secure/gotoDevice.action?hardwareId=1&amp;storeCode=2571&ModeEnabled=false

The issue here is that in IE, the 'setSetCode()' method in the struts action is executed, but not in FF! Removing the hash mark in the 'ahref' tag makes FF work, but it breaks IE (href="#").

I've attempted to change '&ModeEnabled=' to '&amp;ModeEnabled=', but to no avail. I've searched on Google and the struts forum, but haven't found a solution.

I'm considering replacing all 'ahref' with Dojo buttons to see if that resolves the issue, but before I do, I'd like to understand why.

My suspicion is that 'ahref' might not be the correct element to use, but I'm unsure why.

If anyone can shed some light on this issue, I would greatly appreciate it.

Thank you, Jeff Porter

EDIT: Adding 'return false' seems to be part of the solution. The problem appears to be the URL...

/mainApp/secure/setMaintenanceMode.action?hardwareId=5&amp;storeCode=2571&ModeEnabled=true

contains '&amp;' internally. If I visit this URL as is, it works in IE but not in FF.

If I change both instances to '&', it works in both IE and FF.

If I change both instances to '&amp;', it still works in IE but not in FF.

Any thoughts?

Note:

It seems that struts 2.0.9 does not support the 'escapeAmp' property on the '<s:url' tag:

By default, request parameters are separated using escaped ampersands (i.e., '&amp;'). This is necessary for XHTML compliance. However, when using the URL generated by this tag with the '<s:property>' tag, the 'escapeAmp' attribute should be used to disable ampersand escaping.

Solution: Add 'return false' to the 'onclick' and upgrade to a newer version of struts + set the 'escapeAmp' parameter. Alternatively, use 'url = url.replace("&amp;", "&");'.

Answer №1

One way to prevent the javascript onclick event from reaching the browser is to simply return false from the javascript method.

function setMaintenanceMode(enabled) {
    var url = '<s:url action="secure/setMaintenanceMode"/>' + '&ModeEnabled=' + enabled;
    document.location.href = url;
    return false;
}

<a class="button" href="#" onclick="return setMaintenanceMode(false);">disable</a>

By using this approach, the javascript onclick event will be stopped before it can affect the browser.

Answer №2

setAttribute("onmousedown", "enableUpdate(); return false;")

While the onmousedown event is being triggered successfully, the default action is still carried out immediately. To prevent this, ensure that the event handler includes a return false statement to indicate that the default action should not be executed.

Internet Explorer may misinterpret the intended behavior and perform the incorrect action.

Answer №3

There is a peculiar issue with the URL containing the &amp; character. It seems to function correctly in Internet Explorer but fails in Firefox.

This issue is not browser-specific but rather server-dependent. The problem arises from the way the server framework parses parameters. The use of & as a parameter separator is required by Servlet. Therefore, a string like a=b&amp;c=d will result in a=b and amp;c=d. The application, expecting c, will not recognize the latter parameter.

The HTML specification suggests allowing the use of ; as a separator. In that case, the server would interpret the string as a=b, a meaningless amp, and c=d, allowing the URL to still function despite the mis-encoding of the ampersand. Unfortunately, Servlet does not follow this recommendation.

Request parameters are typically separated using escaped ampersands (&amp;).

It is advised not to escape ampersands when joining parameters into a URL. Instead, parameters should be joined with a single ampersand, and the entire URL should be HTML-encoded if necessary. Struts's default behavior in this regard is incorrect.

In your scenario, the URL is being written into a string literal within a script block. HTML-escaping is not required in a script block in traditional HTML, but JavaScript escapes should be considered to prevent syntax errors.

To ensure the URL is properly formatted for JavaScript and does not contain any breaking characters, consider using a JSON encoder. This will handle Java values and escape characters like < and & for JavaScript, avoiding parsing issues in XHTML.

Consider replacing the <a> tags with buttons.

If the element is not a hyperlink but rather triggers a script action, using a <button> or <input type="button"> is more semantically correct. CSS can be used to style the button as needed.

However, replacing links with buttons may not be necessary. Simply adjusting the link's structure could suffice:

<a href="/mainApp/secure/setMaintenanceMode.action?hardwareId=5&amp;storeCode=2571&amp;ModeEnabled=false">disable</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for advice on successfully implementing createMultiMaterialObject in THREE.js version r62?

I am having trouble getting createMultiMaterialObject to work as expected. My goal is to have a wireframe material displayed on top of a solid material. However, the multimaterial function only seems to display the first material in the array. Here is the ...

Acquiring POST parameters within Laravel's Controller from JavaScript or Vue transmission

I am trying to send Form data from a Vue component to a Laravel API using the POST method. Although Laravel is returning a successful response, I am encountering difficulty in handling the POST data within the Laravel controller. Below is the code for th ...

having trouble retrieving 200 status code from Angular server response

Objective: I need to make certain changes to the record locally if the server responds with a 200 code. Problem: I am unable to retrieve the response code or access the 'message' attribute. This is the server response I receive from the HTTP ca ...

Ways to troubleshoot and fix issues with executable paths

Encountering an issue Error message: Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: Despite using the command system.setproperty, I am still facing errors on Chrome. ...

Problems with Key Presses in Form Verification

Yesterday evening, our supervisor called me to report an issue with our app. He mentioned that when he tried to log in using a dummy password, the validation was successful. It seems that clicking on the mouse to authenticate the password was working corr ...

How can I extract an array of objects from a response in Angular?

Arrange array of objects from the received data. Here is the data that I received. [ {name:someName, value:20}, {name:"", value:21} {name:someName, value:25} {name:someName , value:27} {name:"", value:21} {name:someName, value:20} ] I am looki ...

Tips for increasing the size of Material-ui Rating icons

I am currently utilizing npm to develop a widget. I aim to utilize the material-ui Rating component and have successfully integrated it. However, when I embed the widget on a webpage, the html font-size is set at 62.5%, causing the component to appear too ...

What is the most efficient way to retrieve a document from pouchdb that includes the revision attribute?

I am currently developing an application using the electron framework and integrating pouchdb. As certain values in my database are dynamic and constantly changing, I am looking for a way to track these changes without having to create new documents for ea ...

Navigate through JSON data to uncover the tree structure path

In my project, I am working on creating a treeview user interface using the JSON provided below. I have included properties such as node-id and parentId to keep track of the current expanded structure. Next, I am considering adding a breadcrumb UI compone ...

What steps should be followed to effectively utilize the "showPreview" feature of NetExport in an automated script with Webdriver, Firebug, and NetExport?

Currently, I am utilizing Selenium Webdriver in conjunction with Firefox and the Firebug/NetExport plug-ins to carry out regression testing. The specific scenario involves extracting a query string parameter from an HTTP request after a user interacts with ...

Place attribute value directly under the border of an HTML element using a combination of JavaScript and CSS

I'm currently working on a JavaScript script to scan the DOM for elements that have a specific custom attribute called example-type. The goal is to apply CSS styling to draw a border around these elements and then display the value of the example-type ...

Contrasting images showcasing Headless vs Non Headless settings in Puppeteer

I'm currently attempting to capture a screenshot or PDF of the content available at this URL. When using the option {headless: false}, the screenshot is generated correctly; however, in headless mode, some images do not render in the screenshot (for e ...

Enhance your HTML audio player with added timeline functionality

I'm currently working on incorporating an HTML audio player into my project. I've managed to get the play/pause functionality to work, but now I'm stuck on adding a timeline feature. Additionally, I'm not sure how to implement the play/ ...

Discover the Magic Trick: Automatically Dismissing Alerts with Twitter Bootstrap

I'm currently utilizing the amazing Twitter Bootstrap CSS framework for my project. When it comes to displaying messages to users, I am using the alerts JavaScript JS and CSS. For those curious, you can find more information about it here: http://get ...

What is the best way to navigate a LinkedList in Java and append elements to it?

I'm having trouble with my LinkedList implementation when it comes to printing and adding elements. Everything works fine when I add just one or two nodes, but as soon as I try to add more, things start going haywire. For instance, if I add 4 and 3 ...

execute a function upon selecting a radio button

Below is the HTML code that includes two radio buttons. The default checked button is the "lease" option. <input id="quotation_request_payment_option_lease" class="choose_payment_option" name="quotation_request[payment_option]" type ...

Manipulating specific elements within a MongoDB document's array using index values in a Node.js environment

Is there a way to increase the value of a specific element in an array within a MongoDB document using Meteor or nodeJS? For example, consider the following document: { "_id" : "4tP6ewe4Z5kwYA3Je", "name" : "chutia", "address" : "shonir akhra ...

Font destruction in IE occurs when content is loaded via AJAX

Check out this specific website. Click on the orange arrow button and then the backward arrow once more. The paginator is set to display page 1 as default and load content from other pages using AJAX. However, in Internet Explorer, the content of the de ...

Tips on pairing elements from a ngFor processed list with another list using ngIf

If we have a list such as the one shown below: elements = [ { id: 1, name: "one" }, { id: 3, name: "three" }, { id: 5, name: "five" }, { id: 6, name: "six" }, ]; lists = [ { id: 5, name: "five" }, { id: 9, ...

Remove redundant records from Hibernate tables involving one-to-many relationship references

Currently in my program, I am working with three number values (companyName, groupName, hostName) that are received each time the program runs. Upon receiving these values, I group them into one object (Datas) and store them in a List. The objective is to ...