How to add target = "_blank" in case - javascript

I am using Vanilla 2 as forum with buttonbar plugin to create pre tags in the message area.
When clicking on the url button, he generates in the message box, but I would prefer <a href="" target="_blank></a> to generate.
Here is is the .js with the case for the url attribute:
case 'url':
var urlOpts = {};
var thisOpts = $.extend(htmlOpts, {
center: 'href'
});
var hasSelection = $(TextArea).hasSelection();
var NewURL = '';
if (hasSelection !== false) {
NewURL = hasSelection;
delete thisOpts.center;
} else
NewURL = prompt("Enter your URL:",'http://');
urlOpts.href = NewURL;
$(TextArea).insertRoundTag('a',thisOpts,urlOpts);
break;
Can someone tell me how to add the target="_blank" in it?

It seems to be solved so maybe here could be useful for anyone:
I don't know exactly the technologies u are using but have u tried with
urlOpts.target = '_blank';
before
$(TextArea).insertRoundTag('a',thisOpts,urlOpts);

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;

How to setup a proper javascript path using path

I was wondering if anyone can help me correct this code.
I am trying to setup 3 different button menus using var path = window.location.href; for main URL, Russian site and Spanish site.
I seem to have 2 out of 3 working.
I am also sorry if I'm not the best at explaining this but below is what I'm trying to do. Hopefully someone can figure out what I'm doing wrong and help.
var path = window.location.href;
if (~path.indexOf("/ru/")){
$j("#button1").append("<a class=\"ru-text\" href=\"http://#/\"> <span>Отслеживание </span><br/>отгрузки</a>");
}
var path = window.location.href;
if (~path.indexOf("/es/")){
$j("#button1").append("<a class=\"es-text\" href=\"http://#/\"> <span>RASTREAR </span><br/>ENVIO</a>");
}
}else{
$j("#button1").append("<span>Track Your</span> <br/>shipment here");
};
};
You have wrong if-else conditional, it should be as follows:
var path = window.location.href, linkEl = "";
if (~path.indexOf("/ru/")) {
linkEl = "<a class=\"ru-text\" href=\"http://#/\"><span>Отслеживание</span><br/>отгрузки</a>";
} else if (~path.indexOf("/es/")) {
linkEl = "<a class=\"es-text\" href=\"http://#/\"><span>RASTREAR </span><br/>ENVIO</a>";
} else {
linkEl = "<span>Track Your</span><br/>shipment here";
}
$j("#button1").append(linkEl);

Prevent PDF from auto-downloading and have it Auto Print instead

I have a situation where when a user is viewing their order history, they are provided a link to print a PDF version of their invoice. Currently when the user clicks on the link the PDF will automatically download to their computer.
The 'higher-ups' are wanting that to be changed, so when the user clicks the link to print the pdf, a print dialogue box will appear instead of automatically downloading the PDF.
I've been able to get the pdf open in a new window but when ever the pdf is being called it won't display the pdf but will automatically download it instead.
I've searched all over the internet and SO for a solution but have yet to yield any results. This needs to be done, if possible, via JavaScript or jQuery.
HTML
<p><a id="print_pdf" target="_blank">Click to Print Invoice</a>
JavaScript / jQuery
var print_pdf_link = $('#print_pdf').attr('href');
var linkNo2 = "privatefile.dhtml~useridtext~&file=~username~_rep.pdf";
$('#print_pdf').click(function(){
w = window.open(linkNo2);
w.print();
});
I've also used:
<a href="#" onclick="window.open('privatefile.dhtml~useridtext~&file=~username~_rep.pdf', '_blank', 'fullscreen=yes'); return fal
se;">MyPDF</a>
* please note the text between tilde are placeholders that contain values from the back-end
Thanks for your help!
Try it i hope i would make use of.
var pfHeaderImgUrl = '';
var pfHeaderTagline = '';
var pfdisableClickToDel = 0;
var pfHideImages = 0;
var pfImageDisplayStyle = 'right';
var pfDisablePDF = 0;
var pfDisableEmail = 0;
var pfDisablePrint = 0;
var pfCustomCSS = '';
var pfBtVersion = '1';
(function() {
var js, pf;
pf = document.createElement('script');
pf.type = 'text/javascript';
if ('https:' === document.location.protocol) {
js = 'http://domain.com//main.js'
} else {
js = 'http://domain.com/printfriendly.js'
}
pf.src = js;
document.getElementsByTagName('head')[0].appendChild(pf)
})();
<a href=""
style="color:#6D9F00;text-decoration:none;"
class="printfriendly"
onclick="window.print();return false;"
title="Print">Print</a>

Getting Parent URL using document.referrer

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>

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