Getting Parent URL using document.referrer - javascript

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>

Related

Appending current URL parameters onto anchor link

At a page like
https://www.example.com/?firstname=Steven&lastname=Smith&email=steve%40gmail.com&phone=0404555555
I have a button (anchor link) #ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell that links to https://www.example.com/form
I'd like to append the URL parameters from the current URL to the button's URL, so that the button's href is now https://www.example.com/form/?firstname=Steven&lastname=Doig&email=steve%40gmail.com&phone=0404555555
How can I do this with JavaScript please?
Use window.location.search:
var url = "https://exmaple.com";
var newurl = url + window.location.search;
newurl will contain all the get (ex. ?something=something&something2=something5) data.
To change the href of a:
var button = document.getElementById('#ptsBlock_553944');
button.href = button.href + window.location.search;
If you don't care about supporting older browsers you can use the URL API and URLSearchParams.
function appendCurrentUrlSearchParams(anchorElement) {
const currUrlSearchParams = new URL(window.location.href).searchParams;
const link = new URL(anchorElement.href);
// uncomment this line if you want to clear query parameters already present in the anchor url
// link.search = '';
for (const entry of currUrlSearchParams.entries()) {
link.searchParams.append(entry[ 0 ], entry[ 1 ]);
}
anchorElement.href = link.href;
}
Usage in your case:
appendCurrentUrlSearchParams(document.querySelector('#ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell'));
Read Html select using select to change the link of a button with Javascript
specifically the section on
Get the element with something like document.getElement MDN getElement Link
Change the .href of that element to what you want.
function selectFunction() {
var x = document.getElementById("selectopt").value;
document.getElementById("mylink").innerHTML = x;
document.getElementById("mylink").href = "http://www." + x + ".com";
}
document.location.pathname = '/questions/69240453/appending-
current-url-parameters-onto-anchor-link/69240510';
//get the document pathname I chose from document.location
let data = document.location.pathname;
let preUrlString = 'www.example.com/form';
let newString = preUrlString + data;
console.log(newString);
'www.example.com/form/questions/69240453/appending-
current-url-parameters-onto-anchor-link/69240510'
document.getElementById("mylink").href = newString;

windows URLSearchParams when changed

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>

Dynamically generated href won't show properly

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;

set src="id" iframe taking src by url.com/id

i'm tryng to set a src of an iframe taking the url from the toolbar address.
the address if composed by the domain + id (that is the same of the youtube id)
example
www.youtube.com/whatch=id
my domain is = www.domain.com/?id (the id is the same)
this my hypothetically script
<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array[segment_array.length - 1];
alert(last_segment);
var iframe = document.getElementById("youtube");
iframe.src = www.youtube.com/watch=last_segment;
</script>
Maurizio Grasso!
#LGSon is almost right — you have to use quotes while setting iframe.src.
But if your domain is www.domain.com/?id (with question mark) you have to use window.location.search to get URL parameters instead of window.location.pathname, so your script becomes something like:
var id = window.location.search.slice(1);
iframe.src = 'www.youtube.com/watch=' + id;

Converting URL into clickable link using Javascript

I have a text field called 'patentURL' in a form. The user enteres the complete URL into this field while saving the record. When the user searches for this record, the entered URL should be clickable. i.e. in the result page, the entered URL should be clickable.
How do I achieve this using Javascript?
There is a non-standard function, but widely spread - link() MDC
function makeClickable(url) {
return String.prototype.link ? url.link(url) : ''+url+'';
}
function makeDOMClickable(url) {
var link = document.createElement('a');
link.href = url;
link.innerHTML = url;
return link;
}
var url = "http://localhost";
document.write ( makeClickable ( url ) );
document.body.appendChild ( makeDOMClickable ( url ) );
demo
If i understood correctly you should put the url in a link:
URL_ENTERED
With javascript:
var link = document.createElement('a');//create link
link.setAttribute('href', 'URL_ENTERED');//set href
link.innerHTML = 'URL_ENTERED';//set text to be seen
document.body.appendChild(link);//add to body
You can use javascript regular expression to achieve this have a look
function convert()
{
var text=document.getElementById("url").value;
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
var text1=text.replace(exp, "<a href='$1'>$1</a>");
var exp2 =/(^|[^\/])(www\.[\S]+(\b|$))/gim;
document.getElementById("converted_url").innerHTML=text1.replace(exp2, '$1<a target="_blank" href="http://$2">$2</a>');
}
this way you can convert any text into link you can find more detail here http://talkerscode.com/webtricks/convert-url-text-into-clickable-html-links-using-javascript.php
Example to call href in Javascript:
function call_link() {
location.href = 'www.google.com';
}

Categories