I want to replace any text like this in input: [www.Link-to-be-shortened.com]following link(cut)
I want to replace it with following linkby javascript
I have tried this code :
var UserString = " Hi <br>This article is good , Please visit the following link [www.Link-to-be-shortened.com]following link(cut)";
var SystemString = UserString.replace("[", "");
SystemString = SystemString.replace("]following link(cut)", "");
var a = document.createElement('a');
var linkText = document.createTextNode("following link");
a.appendChild(linkText);
a.title = "following link";
a.href = "http://cuer.esy.es/?f="+SystemString;
document.body.appendChild(a);
But this code does not work well
Here's a simple example of how to do this with a regular expression:
var UserString = "[www.Link-to-be-shortened.com]Click here(cut)";
var link = UserString.replace(/\[([^\[]+)\]([^(]+)\(cut\)/g, '$2');
console.log(link);
HOWEVER, this will not work in all possible cases. You could use this if only trusted people are submitting links.
Related
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;
I have this code:
function download()
{
var a = document.body.appendChild(document.createElement("a"));
a.download = "CalExport.svg";
var dd = document.getElementById('SvgResult');
alert(dd.innerHTML); //displays fine
a.href = "data:image/svg+xml," + dd.innerHTML;
a.click();//downloaded file cuts off at the first "#"
}
When the alert displays it it's okay, the downloaded version is cut off before the first "#". How do I fix this?
Since this is part of a href, you need to url-encode your data first, eg.
function download()
{
var a = document.body.appendChild(document.createElement("a"));
a.download = "CalExport.svg";
var dd = document.getElementById('SvgResult');
alert(dd.innerHTML); //should still display fine
a.href = "data:image/svg+xml," + encodeURIComponent(dd.innerHTML);
a.click();//should now not cut off.
}
The safe variation of # in a url is %23%0A (check out this tool: http://meyerweb.com/eric/tools/dencoder/).
I'm fairly new to JavaScript and I have this RSS Feed I'm working with currently.
When I retrieve an item from the RSS feed, the following is displayed
Google Home Page http://www.google.com
How can I split this string, so that I can embed the second part of it (http://www.google.com) into the first part(Google Home Page)?
First - exclude the link by using following RegEx pattern (searches for string which starts with http://).
/http:\/\/.*[^\W+]/g
The matched value (Array) is being stored into url, now we are able to create the anchor element. (the value of href is the element 0 inside our matches array).
The link content is being generated by replacing the URL with empty space inside the retrievedResult. trim() is optional, I've used it just to remove remaining space.
retrievedResult.replace(url[0], "").trim()
Finally you can append the built anchor element.
var retrievedResult = "Google Home Page http://www.google.com";
var re = /http:\/\/.*[^\W+]/g;
var url = retrievedResult.match(re);
var anchor = '' + retrievedResult.replace(url[0], "").trim() + '';
$('body').append(anchor);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Okay, so this will be the string:
var string = "Google Home Page http://www.google.com";
Then we split it:
var split = string.split('http'); // ['Google Home Page ', '://www.google.com']
Then we create an a element:
var a = document.createElement('a');
Then we add the link as the href attribute of your anchor element:
a.href = 'http' + split[1];
And then we add the text as textContent of your anchor element:
a.textContent = split[0];
And finally we add the element to the body:
document.body.appendChild(a);
Here an example:
var string = "Google Home Page http://www.google.com";
var split = string.split('http');
var a = document.createElement('a');
a.href = 'http' + split[1];
a.textContent = split[0];
document.body.appendChild(a);
You can use jquery to get to your result
Working Example:
//This is HTML part
<div id="linkcontainer"></div>
<input id="str" value='Google Home Page http://www.google.com'>
<a id="createlink">CreateLink</a>
//This is js part
$('#createlink').click(function(){
createLink();
});
//function that makes link
function createLink(){
var str = $('#str').val();
var http = str.indexOf('http');
var url = str.substring(http);
var text = str.substring(0,http);
$('#linkcontainer').html(''+text+'');
}
Try this code on jsfiddle
I'm trying to create a link within javascript, which when clicked, sets the value of a public render parameter within WebSphere Portal.
For some reason I see actual code in the generated href instead of value thats suppose to be passed in.
Heres how I create the link within javascript ..
var a = document.createElement('a');
var linkText = document.createTextNode('This is a link');
a.appendChild(linkText);
a.title = 'This is a link';
a.href = '[Plugin:RenderURL pr1.mode="set" pr1.type="public" pr1.key="sources" pr1.value=' + JSON.parse(http.responseText).uniqueID + ']';
This is what I see in the Firefox Inspector devtools ..
This is a link
I understand this is likely an escaping issue, but i've tried so many variations, how should I properly create the href for my link?
Sample for setting prp using a form, same idea as anchor above ..
// create form to set PRP
var f = document.createElement('form');
f.setAttribute('method', 'GET');
f.setAttribute('action', '[Plugin:RenderURL copyCurrentParams="true"]');
var filters = document.createElement('input');
filters.setAttribute('type', 'hidden');
filters.setAttribute('name', 'sorting');
filters.value = 'This is my new value!';
var s = document.createElement('input'); //input element, Submit button
s.setAttribute('type', 'submit');
s.setAttribute('value', 'Set');
f.appendChild(filters);
f.appendChild(s);
document.body.appendChild(f);
f.submit();
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';
}