My English is not good sorry,
I do not want to let one of the URLSearchParams change.
And when the URLSearchParams is changed, returne to the my input value.
URL address : example.com/action.php?id=1&name=john&penalty=365
<input name='penalty' value='365' hidden>
<script>
$(document).ready(function() {
const url = window.location.href;
const paramspenalt = new URLSearchParams(url.split('?')[1]);
var penaltyvar = $('input[name=penalty]').val();
paramspenalt.set('penalty', penaltyvar);
const resultpenalty = paramspenalt.toString();
window.location = 'action.php?'+resultpenalty+'';
});
</script>
Everything is fine with this code, But the page is constantly loading.
It is very good if the page is loaded only once.
#ksav helping me and
fixed my problem from How do I modify the URL without reloading the page?
:
<input name='penalty' value='365' hidden>
<script>
$(document).ready(function() {
const url = window.location.href;
const paramspenalt = new URLSearchParams(url.split('?')[1]);
var penaltyvar = $('input[name=penalty]').val();
paramspenalt.set('penalty', penaltyvar);
const resultpenalty = paramspenalt.toString();
//****
window.history.pushState("object or string", "Title", 'action.php?'+resultpenalty+'');
//****
});
</script>
Related
Can someone explain to me this code snippet?
<button id="btn6" href="javascript: void(0)" onclick="function1()" type="submit">Get Link</button>
<script>
function function1() {
var Link = 'https://google.com';
var referr = '';
{
window.location = Link+''+referr;
}
}
</script>
I want to find the complete URL(with parameters) that is sent to the browser when clicking the button
As Andy said, there is a lot of issues with your code but once you fix it, use the URL object that is provided in modern browsers and the searchParams function :
var url = "https://google.com/test.html?a=1&b=test-param-sample-123"; //for exemple
var url = new URL(url);
var b = url.searchParams.get("b");
console.log(b); //return the b parameter "test-param-sample-123"
I let you check this article for more informations
I am working on a bookmarklet (currently just javascript code) that will prompt the user, asking for a domain name, and whatever domain name they enter will change the favicon of the current site they are on to the one of the domain they entered. I have gotten the code to work without errors, but the favicon doesn't change.
The code does not error, it simply prints "Favicon changed" in the console, but nothing changes.
here is my code:
var script = document.createElement('script');
script.src = '//code.jquery.com/jquery-3.4.1.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
var waitForJQuery = setInterval(function () {
if (typeof $ != 'undefined') {
function changeFavicon(src) {
$('link[rel="shortcut icon"]').attr('href', src);
}
let url;
url = prompt("Please enter the URL you wish to mask this page as. Example: www.google.com", "www.google.com");
changeFavicon("//www.google.com/s2/favicons?domain=" + url);
console.log("Favicon Changed!");
clearInterval(waitForJQuery);
}
}, 10);
Edit: I would like to let it be known I am very inexperienced with javascript, I originally came from Python so I know a bit but a lot of this code may be inefficient or defective. if that is the case feel free to comment or message me about it! Thanks :D
Edit 2: I have tried some other code, this time eliminating jquery.
function changeFavicon(src) {
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
let url;
url = prompt("Please enter the URL you wish to mask this page as. Example: google.com", "google.com");
iconURL = "//www.google.com/s2/favicons?domain=" + url
changeFavicon(iconURL);
It still does not work though.
Consider the following.
$(function() {
function getIcon(url) {
var myData = [
"//www.google.com/s2/favicons?domain=" + url
]
if (url) {
$.get(myData[0], function(results) {
console.log(results);
myData.push(results);
});
}
return myData;
}
$("#get-btn").click(function() {
var newIcon = getIcon($("#url").val());
$("<div>").appendTo($(".prompt"));
$("<img>", {
class: "shortcut icon",
src: newIcon[0]
}).appendTo($(".prompt > div"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="prompt">
<p>Please enter the URL you wish to mask this page as.</p>
<input type="text" id="url" placeholder="www.google.com">
<button id="get-btn">Get FavIcon</button>
</div>
I have found the thing that was wrong!
www.google.com/s2/favicons?domain= does NOT Return an ico file so it failed to work because of so.
So I'm trying to make this link appear on my page, but it won't return the /register path, it'll just go immediately the UTMs.... On the site it'll show the href as
domain.com/?utm_campaign...
instead of
domain.com/register?utm_campaign...
Why is that and how can that be fixed?
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
// get the required parameter
const campaign = urlParams.get('utm_campaign');
const source = urlParams.get('utm_source');
const medium = urlParams.get('utm_medium');
var registerationURL = new URL('../register?utm_campaign=&utm_source=&utm_medium=');
registerationURL.searchParams.set('utm_campaign', campaign);
registerationURL.searchParams.set('utm_source', source);
registerationURL.searchParams.set('utm_medium', medium);
var a = document.getElementbyID('test').innerHTML;
a.href = registerationURL;
</script>
<a id="test" href="#">Click here</a>
document.getElementbyID('test').innerHTML returns the string "Click here". Remove the .innerHTML and it should work.
However, this can be done much simpler with the following
var registrationUrl = location.origin + '/register' + location.search;
Is it possible to fill <h1 id="fillname"></h1> with "John" or anything if I add ?fillname=John or ?fillname=anythingausertype in my URL?
Eg.: https://www.example.com/?fillname=John fills <h1 id="fillname">John</h1>
You can, but i do not think this is a good idea. I think there are better ways to accomplish what you want.
Do do this, first get the parameter:
var url = new URL(window.location.href);
var fillname = url.searchParams.get("fillname");
then add it to the h1 tag:
document.getElementById("fillname").innerHTML = fillname;
References:
How to get the value from the GET parameters?
https://www.w3schools.com/jsref/prop_html_innerhtml.asp
You should use window.location.search and the URLSearchParams helper object to look at URL parameters in JavaScript:
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<h1>No parameters provided</h1>
<script>
var h1 = document.querySelector('h1');
var urlParams = new URLSearchParams(window.location.search);
if(urlParams.has('fillname')) {
h1.innerText = urlParams.get("fillname");
}
</script>
</body>
</html>
Try this in a browser and add '?fillname=test' to the end of the filename in the address bar.
Without any external library
const query = window.location.search.replace("?", "");
const parts = query.split("&");
const fillNameQS = parts[parts.findIndex(qs => qs.includes("fillname"))];
const value = fillNameQS.split("=")[1];
document.getElementById("#fillname").innerText = value;
Use split to separate and create array
const url = "https://www.example.com/?fillname=John";
const param = url.split('?')[1].split('=');
console.log(`<h1 id="${param[0]}">${param[1]}</h1>`)
Reference: Split
uri = window.location.search.split('=')
if (uri[0] === '?filename'){
document.getElementById('fillname').innerText = uri[1]
}
I'm trying to figure out how to get the second slash or page of a website using the document.referrer javascript
for example the website is www.mysite.com/page1/subpage/subpage2/
I only need to get www.mysite.com/page1/
var url = document.referrer;
var referrer = url.match(/:\/\/(.[^/]+)/)[1];
this only gets me the domain. I need the second page. Any help would be great.
Thanks
Hope this does what you need:
function getReferrerSecondPage() {
var link = document.createElement("a");
link.href = document.referrer;
return link.pathname!="" ? [link.host, link.pathname.split("/")[1]].join("/") : link.host;
}
var referrerSecondPage = getReferrerSecondPage();
Modified your code:
var url = document.referrer;
var referrer = url.match(/(http:)\/\/(.[^/]+)\/([^./]+)/)[0];
// Display
var foo = document.getElementById("foo");
foo.innerHTML = referrer + "/";
<p id="foo"></p>