I am making a website where you have to input a link, and it makes javascript, but after imputing the variable, it adds a line so it doesn't work.
This is the code I am using
var url = prompt("Enter the link you want the app to go to.");
document.getElementById(2).onclick = "window.open('" + url + "', '_blank');'>";
Anyone got suggestions?
You just dont need to use these strings to set url variable in window.open method.
You could try this:
var url = prompt("Enter the link you want the app to go to.");
document.getElementById(2).onclick = () => window.open(url, '_blank');
I think what you want to do is something like that
var url = prompt("Enter the link you want the app to go to.");
if(url){
document.getElementById("xxxx").onclick=function(){
window.open(url, '_blank');
}
}
Quickest fix: First remove that stray <, as #audiodude said. Then use the function constructor.
var url = prompt("Enter the link you want the app to go to.");
document.getElementById(2).onclick = new Function("window.open('" + url + "', '_blank');")
Related
Well, on my site I have a FAQ where users can access specific questions through the URL, such that:
https://example.com/faq#question-1
And for this I use Jquery, such that:
if (window.location.hash) {
$(window.location.hash).open();
}
But I want that if the user enters more than one hash in the URL such that:
https://example.com/faq#question-1#question-2#question-3#question-n
All hashes are removed from the URL except the first #question-1
How can I do this?
Edit:
if user type https://example.com/faq#question-1#question-2#question-3#question-n then change the url to https://example.com/faq#question-1 so that only the first hash appears in the url and the FAQ only has to read a single hash.
It could something as simple as
var hash = window.location.hash;
var filtered_hash = '';
if(hash.length > 0){
filtered_hash = '#' + hash.split("#")[1];
window.location.hash = filtered_hash;
}
console.log(filtered_hash);
This should work for you
var str="https://example.com/faq#question-1#question-2#question-3#question-n";
var trimmedStr = str.split("#").slice(0,2).join("#");
console.log(trimmedStr);
This removes all hashes after the first hash.
You can use something like -
location.hash = '#'+location.hash.split('#')[1]
Say I have a function:
function potato(param) {
document.body.style.backgroundImage = 'url(file:///C:/Users/Joe)';}
and I want to make it so when I call potato('/chicken.jpg'), it will modify the url in potato to be file:///C:/Users/Joe/chicken.jpg. How could this be done? Thank you
Javascript in a browser doesn't have access to the file system. Try running a local web browser with a document root of C:\User\Joe and then pass in the url path ... something like 'http://localhost/chicken.jpg'
Just use concatenate operator. So in JavaScript use +.
'my/base/url' + myStringVar;
'url(file:///C:/Users/Joe'+param+')';
I think you want something like this. This code will replace the background image when you call the function with the param :
function potato(param){
var $element = document.querySelector('.something'),
url = 'file:///C:/Users/Joe/' + param;
$element.style.backgroundImage = "url('" + url + "')";
console.log('=>' + $element.style.backgroundImage);
}
This is live example on jsbin => https://jsbin.com/?html,console,output
I am sending an email via javascript. In mail.parameter.text property I need to send the hyperlink. In the code below I am hardcoding the url whick looks very lengthy and also I need to add the /dashboard at the end of the url. Any idea on how to shorten the url?
var parent = space.getParent();
var siteGroup = "GROUP_EMAIL_CONTRIBUTORS";
var mail = actions.create("mail");
mail.parameters.to_many = siteGroup;
mail.parameters.subject="New Site Created in Community"
mail.parameters.text=" A new site called " + document.properties.name +"is created.Click http://pc1:8080/share/page/site/"+document.properties.name+"/dashboard"+" to join the site";
//execute action against a document
mail.execute(document);
Sorry if this is a basic question I dont know javascript. This is my entire script and I am not using any html tags in between.
You can change it to something like this, for example:
mail.parameters.text="Click <a href='http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard'>here</a>";
Your link will appear as "here" and nevertheless lead to the URL you specified. Is that what you mean by "shorten the url"?
Please try now.
var url = "Click http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard" + " to join the site";
ail.parameters.text = url;
I'd like to redirect a page with javascript using the following code:
var s = 'http://blahblah/' + encodeURIComponent(something);
alert(s);
window.location.href = s;
The alert shows the correct encoded url but when I pass it to window.locaion.href, it redirects the page to the unencoded url which is wrong.
How could I do it properly?
Thanks
This could be related to (a) using firefox or (b) specific APIs that you're feeding encodedComponent into, like Google search.
Here's one tested solution on Firefox-stable:
var clearComponent = 'flowers for my boyfriend & husband on valentines';
var encodedComponent = encodeURIComponent(clearComponent);
var googleSafeComponent = encodedComponent.replace(/%20/g,'+'); // replaces spaces with plus signs for Google and similar APIs
var completeURI = 'http://google.com/?q=' + googleSafeComponent;
window.location = completeURI;
Or all in one line:
window.location = 'http://google.com/?q=' + encodeURIComponent('flowers for my boyfriend & husband on valentines').replace(/%20/g,'+');
window.location implies window.location.href so you can save some letters. ;)
I want to reload a page using JavaScript passing different parameters in URL.
My Page name is test1.aspx, I used:
window.location="test1.aspx?user=abc&place=xyz";
It's not working...!!!
window.location is an object. You need to access the href property on it, like this:
window.location.href="test1.aspx?user=abc&place=xyz";
If you need to send dynamic value, then
var user = "abc";
var place = "xyz";
window.location.href = "test1.aspx?user=" + user + "&place=" + place;
window.location = "path page?user=" + $("#txtuser").val();