Home >Web Front-end >Front-end Q&A >javascript remove question mark
Preface
When we use JavaScript for data interaction, we often encounter a problem-the URL contains a question mark (?), which may cause errors in our program, so it needs to be dealt with. This article will introduce several methods to remove question marks in URLs and help readers solve similar problems.
1. Problem description
Usually when we send a GET request to the server, the URL will contain a question mark (?). For example: http://example.com?id=1&name=Zhang San. This URL contains a question mark to separate the URL and query string.
However, in some cases, we want to remove the question mark, and then we need to deal with it. For example, when jumping to a page, you may need to remove the question mark in the URL to ensure the correct page jump.
2. Solution
1. Use the substring() method to remove the question mark
It is a common method. Using JavaScript's string function substring(), you can intercept the substring of the URL and remove the question mark. An example is as follows:
let url = "http://example.com?id=1&name=张三"; let newUrl = url.substring(0, url.indexOf("?")); console.log(newUrl);
The output result of this code is: "http://example.com".
In the above code, we first declare a URL, and then use the substring() function to remove the question mark. The substring() function receives two parameters. The first parameter is the starting position of the string to be intercepted, and the second parameter is the end position of the string to be intercepted (excluding the characters at the end position).
In the above code, we use the indexOf() function to find the first occurrence of the question mark. This function returns the position of the substring, or -1 if not found. Using the substring() function, we get the characters before "?", which is the URL address before the question mark.
2. Use the replace() method to remove the question mark
The replace() function is another common method. It replaces a substring in a string with another string. The example is as follows:
let url = "http://example.com?id=1&name=张三"; let newUrl = url.replace("?", ""); console.log(newUrl);
The output result of this code is: "http://example.comid=1&name=Zhang San".
In the above code, we use the replace() function to replace the question mark (?) with an empty string (""). The result of this method is the URL string with the question marks removed.
However, this method will replace all question marks in the URL with empty strings, so it may cause errors. So this method only works when there is only one question mark in the URL.
3. Use the split() method to remove the question mark
The split() function can split a string into a string array and return the array. When processing URLs, we can use the split() function to separate the question marks and take the first element. An example is as follows:
let url = "http://example.com?id=1&name=张三"; let newUrl = url.split("?")[0]; console.log(newUrl);
The output result of this code is: "http://example.com".
In the above code, we use the split() method to split the string into two parts: URL and query string. Then we take out the first part, which is the URL address with the question mark removed.
Summary
The above are several ways to remove question marks in URLs using JavaScript. When we encounter problems when sending GET requests, it is important to make modifications to ensure the normal operation of the program. Through the introduction of this article, I believe readers have learned how to quickly deal with this problem.
The above is the detailed content of javascript remove question mark. For more information, please follow other related articles on the PHP Chinese website!