JS: Append variable to URL without repeating [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change URL parameters with jQuery?
Currently, I have a <div> tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes or &view_all=No to the URL.
<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>
Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes or &view_all=No to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.
I have figured out a way to do the append but it keeps adding the &view_all variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No.
This is the way I am doing the append:
onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"

You can use regular expression to replace the value in a string:
$(this).attr("onclick", function(i, val) {
return val.replace(/view-all=(Yes|No)/, function() {
return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
});
});

Related

How to check for a specific word in the URL before running script? [duplicate]

This question already has answers here:
How to make a script redirect only once every time an appropriate page loads?
(4 answers)
Closed 3 years ago.
I have the current script to amend a link in the URL bar
(function() {
'use strict';
location.replace(location.href.replace("www.reddit.com", "old.reddit.com"));
})();
I'd want it to check if the URL has the word "old" before running the remainder of the script. How do I do this?
You can check the index of the word old by considering the URL as a normal string.
if(location.href.indexOf('old') > -1) {
// do something
}
It is simple as that.

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

Create a Tampermonkey script [duplicate]

This question already has answers here:
How to change the name field of an <input> in an AJAX-driven page?
(1 answer)
How to change a browsed page's <input>?
(1 answer)
Closed 4 years ago.
I want to create a Tampermoney script that writes something in an input field, if it finds a string on the page. E.g:
If the page contain the string "life is", the script should automatically append "good".
(I want to make this with word detection, so the string "life is", will appear on the page after that the page was accessed)
First you need to get the body as String and the input you want to modify
<input id='input-id' />
const page = document.body.innerHTML
const input = document.getElementById('input-id')
Then check if the value you want is in the page
if(page.search('life is') !== -1)
input.value = 'life is good'

Show Parameter Segment of URL using JavaScript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 5 years ago.
So I am trying to get the last part of a link and show it in a div on the HTML.
I'm using JavaScript and HTML to perform this; here's what I have so far:
<script type="text/javascript">
var pageURL = window.location.href;
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
document.getElementById("getLink").innerhtml = lastURLSegment;
</script>
<div id="getLink"></div>
And here is my link:
https://playmafia.000webhostapp.com/world/play/create/?id=m12345
However, after executing this, nothing happens or shows. And plus, this code, as far as I know, will retrieve "?id=m12345".
I am new to javascript so any help would be great in figuring this out. Also, this may need jQuery, I'm not completely sure, so I will accept answers that involve it.
Once again, how can I get the parameter of the URL (in the example, m12345) to display in the HTML under the div with id "getLink"? Thanks to anyone who can help.
Try This :
var pageURL="https://playmafia.000webhostapp.com/world/play/create/?id=m12345";
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
var value=lastURLSegment.substr(lastURLSegment.lastIndexOf("=")+1);
document.getElementById("getLink").innerHTML = value;
<div id="getLink"></div>
Try by changing document.getElementById("getLink").innerhtml to document.getElementById("getLink").innerHTML
I hope you need some changes in your code
document.getElementById("getLink").innerHTML = lastURLSegment;
TRY THIS ...

How do I read #2 at the end of a URL? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you check for a #hash in a URL using JavaScript?
Currently I am generating Urls that look like this:
InspectionPhotos.aspx?inspectionId=10001649#/2
The #2 is for a photogallery plugin, and this would mean go the second photo.
I would like to show a div only if there is a #/[anynumber] but if its just
InspectionPhotos.aspx?inspectionId=10001649
then not show anything.
How could I do this check? Either asp.net on pageload or a client side javascript would be fine.
Thanks.
You can't do this in server side, because the hash is not sent to the server, to get this value with javascript is simple:
var hash = window.location.hash;
if (hash){
//use the hash value.
}
JavaScript: window.location.hash will comeback with #/2 from your example.

Categories

Resources