What is the best method to include a "#" in a URL using JavaScript?

What is the reason for getting '/false' when using # in a URL with JavaScript?

var el = '#string with number character -> #'   
el = el.replace(/[']/g, ''');
el = el.replace(/[#]/g, ''');

xmlhttp.open("GET","process_add_article.php?&title=" + (el),true);
xmlhttp.send(); 

Answer №1

When it comes to encoding a string within a URL, there is actually a built-in method for accomplishing this task:

let text = '#text with character -> #';
text = encodeURI(text);

It's important to note that simply encoding the string may not yield the desired result. After encoding, the resulting URL will appear as follows:

xmlhttp.open("GET","process_add_article.php?&title=#text%20with%20character%20-%3E%20#", true);

This ultimately leads to the title parameter being empty since the server typically disregards anything following the hash symbol (#).

Answer №2

The reason lies within the usage of # as a fragment identifier. These identifiers are strictly client-side components and are not transmitted to the server. For more information, you can refer to Wikipedia.

Answer №3

When dealing with URIs, remember to use Numeric Character Reference instead of Percent-Encoding.

A helpful tool to consider is the encodeURIComponent method for encoding URI components.

var el = '#string with number character -> #'; 
xmlhttp.open("GET", "process_add_article.php?&title=" + encodeURIComponent(el), true);
xmlhttp.send(); 

Be cautious not to mix up encodeURI, as it does not encode certain characters like #, +, and =.

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

What is the method for transmitting a concealed attribute "dragable" to my component?

Currently, I have successfully integrated a here map into my project, but I am now tackling the challenge of adding draggable markers to this map. To achieve this, I am utilizing a custom package/module developed by my company. This package is designed to ...

Type in "Date" and select a date using your mobile device

Hey, does anyone know a good solution for adding placeholder text to an input with type="date"? I'm looking for a way to utilize the phone's built-in date selection feature on a website, rather than having users manually enter dates using the ke ...

Having trouble getting Vuejs to work when appending an element to Fullcalender

Hi there, I am facing an issue where appending a custom button to a full calendar event is not working properly with Vue.js methods. It works fine with core JavaScript, but I really want it to work with Vue.js methods. Any ideas on how I can achieve this? ...

Issue with Bootstrap alert persisting even after being displayed once

Having trouble with alert messages in jQuery where I display the message, validate data, and then try to hide it again. No errors in the console, even when logging before and after the process. SOLUTION $('#password, #confirm_password').on(& ...

Mysterious Source within AngularJS $resource

I have been encountering the error of unknown provider, and have explored various solutions but I feel like there is something missing: app.js var myApp = angular.module('myApp', [ 'ngResource', 'myAppControllers', ...

Adjust the size of the pop-up window according to the dimensions of the content

I need help determining the width and height of the body in an HTML page that has dynamically changing content. I want to resize the window based on the amount of content on the page. Can anyone suggest a solution using JavaScript? Thank you! ...

Encountering an error while attempting to run bcrypt on Meteor and Nodejs - "Undefined property '_handle'."

Having trouble integrating the bcryptjs package into my Meteor app. Successfully installed bcrypt using meteor npm install --save bcrypt. Trying to implement bcrypt functions with import bcrypt from 'bcrypt';. Encountering an error in the cons ...

The basic Node.js API for greeting the world encountered a connection failure

I'm currently working on setting up a basic hello world route using nodejs and express. After running node index.js, I see listening on port 3000 in the console, but when I attempt to access http://localhost:3000/helloworld, it just keeps trying to co ...

Scrolling text blocks on mobile devices

When viewing the website on a desktop, everything works perfectly. However, when accessing it on a mobile device and trying to scroll down, only the text moves while the page remains stationary. The website utilizes skrollr core for animations. I have alre ...

the sorting functionality failing to switch between ascending and descending orders

I have been diligently working on a table that organizes cat data using pure JavaScript and jQuery. Although I am close to completion, I am struggling with implementing sorting functionality for my table headers in both ascending and descending order. I ha ...

Looking to add a form within another form using AJAX? Just keep in mind that the initial form should also be inserted using AJAX

I have incorporated a form using AJAX on a Php page. Now, I am trying to add another form within that existing form which is also created using AJAX, but unfortunately, it is not functioning as expected. This serves as the parent page. <div class=" ...

Is there a way to execute the identical promise once more based on the outcome of the previous execution?

My dilemma involves a block of code enclosed within a promise. This particular code is responsible for modifying records in a remote database and reporting the number of records edited. In case the count of edited records exceeds 0 (indicating work done), ...

Issues with react-bootstrap component Switches functionality not operating as expected

It looks like the bootstrap switches are not functioning properly. Even the documentation version is not working as expected <Form> <Form.Check type="switch" id="custom-switch" label="Check this switch" /> <Form.Check ...

What is the best way to preserve the allocated space in the DOM while utilizing CSS display:none?

I have explored the functionalities of display:none and visibility:hidden. However, I need to utilize display:none for a specific reason while still preserving the element's allocated space in the DOM to prevent any impact on adjacent elements. Is thi ...

On my webpage, there are two buttons labeled "Save" and "Complete". I am in need of creating two distinct alerts for each of the input fields

If the user does not enter a value in the input box for charge_amt, I have set up an onbeforeunload jQuery event to notify them. Once they enter the amount, they will be able to save. <td><input type="hidden" name="charge_cc" value="0" class=""&g ...

How to efficiently manage Bootstrap tabs that share the same ID

I am looking to consolidate two pieces of content with the same ID into one location. Currently, I have tabs for home, menu1, and menu 2, each with their own respective content tabs under menu 1. My goal is to combine both instances of menu 1 content into ...

What is the best way to replace a double quotation mark within a string that is already enclosed in double quotation marks using JavaScript

How can I handle a double quote (") within double quotes while using a regular expression in the JSON string provided below? [{ "LDAP_ID":"a", "LAC_NO":"1153274", "ACTION":"VBE", "DATE_OF_ACTION":"06-01-2006 AM 12:00:00", "RESPONS ...

Using Vue Draggable in conjunction with VueJS V-if directive

I am facing the challenge of integrating a list with a v-if directive in a Vue-Draggable component. The scenario is that users can rearrange items in a lengthy list, as well as 'hide' certain sections of that list. The issue arises when hidden i ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

What is the mechanism behind the functioning of Async/await and why is it not operational in this scenario

I've been struggling to grasp the concept of async/await and how it functions. My main goal is to make it wait until the desired value is returned, but I have encountered challenges with callbacks, promises, and async/await. What mistakes am I making, ...