Javascript - reload page with different QueryString - javascript

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();

Related

How to make the variable not start new line

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');")

JQuery Using passed parameter value to initialize a page via $(document).ready

I am trying to access or make a parameter value which was passed via an <a> visible within another javascript file. I can't figure out how to go about it.
I have the ff. line of code which goes to a new page: student_learn_topiclink.php everytime the link is clicked
var topicLink = $('<a>' + topicTitle+ '</a>').addClass('topic_link').attr('href','view/student_learn_topiclink.php?topicId=' + topicId).attr('target','_blank');
This is what shows in the address bar after clicking the topicLink
http://localhost/cai/view/student_learn_topiclink.php?topicId=5
I want to use the value 5 in student_learn_topiclink page's JS file.
How can I expose or make the topicId = 5 visible in student_learn_topiclink JS file?
In student_learn_topiclink.js, I'd like to do something like this,
$(document).ready(function(){
alert(topicId);
});
See this thread about retrieving URL parameters with javascript. For your specific example, try the following:
var url_string = window.location.href;
var url = new URL(url_string);
var topicId = url.searchParams.get("topicId");
console.log(topicId);
Example fiddle: http://jsfiddle.net/craftman32/yxrvbcun/1/
You can use like this :
var topicId = window.location.search.split('topicId=')[1];
console.log(topicId);

How do I add the parameter of a javascript function into a url?

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

Javascript get url and redirect by adding to it

I have a situation where I need to get the current page url and redirect to a new page by adding to the current page url. For example:
http://www.mywebsite.com/page1/page2
needs to redirect to:
http://www.mywebsite.com/page1/page2/page3
I need it to be relative because "/page1/page2/" will always be different, but "page3" will always be the same.
I've tried: location.href = "./page3"; but that does not work. The result is:
http://www.mywebsite.com/page1/page3
Any thoughts?
Maybe this?:
location.href = location.pathname + "/page3";
Get and Set URL using either window.location.href or document.URL;
window.location.href = window.location.href + "/page3";
Should do what you're looking for.

How to add an variable value in between the hyperlink in javascript

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;

Categories

Resources