I am Passing two variable parameters in #Url.Action(). But its not accepting the variable parameters but accepting constant values. My code is like this
$(document).ready(function () {
$(".print").click(function () {
var id = $(#id).val();
var dept = $(#dept ).val();
var url = "#Html.Raw(#Url.Action("ActionResultname", "ControllerName", new {sid= id , sdept=dept }))";
window.location.href = url;
});
});
here id & sid is not accepting as a value , its taking as string . But if i pass like
var url = "#Html.Raw(#Url.Action("ActionResultname", "ControllerName", new {sid= 10, sdept=30}))";
Its accepting correctly as sid=10 & sdept=30. Please help me how to pass a variable to the parameter.
You can't pass JavaScript variable to Url.Action as it is execute on server side. further Url.Action function will render a string.
However, You can use generate url with static value and replace it with your input.
//Render Url with -1 and -2 value
var url = '#Url.Action("ActionResultname", "ControllerName", new { sid = -1, sdept= -2})';
url = url.replace(-1, id); //replace -1 with id
url = url.replace(-2, dept); //replace -2 with dept
window.location.href = url;
In your case I would suggest just writing out the link you want as Razor code runs server-side so it won't get updated with the values you want.
How about you change your code to:
$(document).ready(function () {
$(".print").click(function () {
var id = $(#id).val();
var dept = $(#dept ).val();
var url = "/ControllerName/ActionResultname?sid=" + id + "&sdept=" +dept;
window.location.href = url;
});
});
This not possible you need to append the query through client side
try this
$(document).ready(function () {
$(".print").click(function () {
var id = $('#id').val();
var dept = $('#dept').val();
var url = '#Url.Action("ActionResultname", "ControllerName")';
url += "?sid= " + id + "&sdept=" + dept;
window.location.href = url;
});
});
Related
I have this in my js:
var data = "newValue";
$('#myButton').on('click', function() {
var params = location.href.split('?')[1].replace(/#.*/g,'');
location.hash = 'myhash';
location.search = params + '&newData='+ data;
});
it always change the url I can see in the browser, but it's not always launched.
what is my problem?
To change the value of search parameter in angular, you just need to do:
$location.search('someVar','newValue');
I'd like to add a parameter after an URL in a Javascript function.
The full URL I'd like to have is: https://my-url.com/section1/section2/here_a_random_number.json?lang=en
This is the ".json?lang=en" that I'd like to add at the end of the URL.
Here my function (in a Google script, linked to a sheet):
function myfunction(randomnumber) {
var myUrl = "https://my-url.com/section1/section2/" + escape(randomnumber);
var jsonData = UrlFetchApp.fetch(myUrl);
var jsonString = jsonData.getContentText();
var jsonObject = JSON.parse(jsonString).result;
var name = (jsonObject.name);
Utilities.sleep(2000);
return name;
}
Where could I put/add my language parameter ?
Just add it to the end of the URL:
var myUrl = "https://my-url.com/section1/section2/" + escape(randomnumber) + ".json?lang=en";
try to replace
var myUrl = "https://my-url.com/section1/section2/" + escape(randomnumber);
by
var myUrl = "https://my-url.com/section1/section2/" + escape(randomnumber)+".json?lang=en";
I went through sb code and wants to implement similar code. he used:
htmlItems += '<li><a href="show-feed.html?url=' + items[i].url + '">' +
items[i].name + '</a></li>';
and used this javascript code to retrive the url and parse to a method
.on('pageinit', '#show-feed-page', function () {
var url = this.getAttribute('data-url').replace(/(.*?)url=/g, '');
Application.initShowFeedPage(url);
it works well and i want to parse three values to the method eg
<a href="showProduct.html?code='+ items[i].code +',name='+items[i].name+',price='+items[i].price+'">"and need code to retrive and parse to a method
initShowProductPage(code,name,price);
First of all, your html is wrong, you have to prepare proper query string format, the updated html mark up is:
<a href="showProduct.html?code='+ items[i].code +
'&name='+items[i].name+'&price='+items[i].price+'">"
You have to access window.location.href and parse it for the query string parameters.You can write a method which parses the url, like below:
function parseURL() {
var vars = [];
var hashes = window.location.href.slice( window.location.href.indexOf('?')+1 ).split("&");
for (var i=0;i<hashes.length;i++) {
hash = hashes[i].split("=");
vars.push( hash[0] );
vars[ hash[0] ] = hash[1];
}
return vars;
}
Then you can access them using code, name and price parameters like below:
.on('pageinit', '#show-feed-page', function () {
var hashObj = parseURL();
// To get code
var code = hashObj["code"];
// To get name
var name = hashObj["name"];
// To get price
var price = hashObj["price"];
// Now call the method
initShowProductPage(code,name,price);
});
I would like to redirect a user to a target URL on a button click. The target URL is variable and has to be read from the current page URL parameter 'source':
For instance, I have a url http://w/_l/R/C.aspx?source=http://www.google.com
When the user clicks on a button he's being redirect to http://www.google.com
How would I do that with jQuery?
first of all you need to get the url param : source
this can be done with a function like :
function GetParam(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
// you can use it like
var source = GetParam('source');
//then
window.location.href = source
On button click handler, just write window.location.href = http://www.google.com
You will need to parse the query string to get the value of the variable source.
You don't need jQuery for it.
A simple function like this will suffice:
function getFromQueryString(ji) {
hu = window.location.search.substring(1);
gy = hu.split("&");
for (i = 0; i < gy.length; i++) {
ft = gy[i].split("=");
if (ft[0] == ji) {
return ft[1];
}
}
}
location.href = getFromQueryString("source");
Using the url parsing code from here use this to parse your url (this should be included once in your document):
var urlParams = {};
(function () {
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.search.substring(1);
while (e = r.exec(q))
urlParams[d(e[1])] = d(e[2]);
})();
Then do this to redirect to the source parameter:
window.location.href = urlParams["source"];
Since you are using the jQuery framework, I'd make use of the jQuery URL Parser plugin, which safely parses and decodes URL parameters, fragment...
You can use it like this:
var source = $.url().param('source');
window.location.href = source;
get url params : (copied from another stackoverflow question) :
var params= {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
params[decode(arguments[1])] = decode(arguments[2]);
});
window.location = params['source'];
You can do like this,
<a id="linkId" href=" http://w/_l/R/C.aspx?source=http://www.google.com">Click me</a>
$('#linkId').click(function(e){
var href=$(this).attr('href');
var url=href.substr(href.indexof('?'))
window.location =url;
return false;
});
I want to know if the url is relative or no using javascript. Basically i will be passed the url, if the url is relative append the current url i.e minus the file name. Can some one help me with this
eg:-
CURRENT URL = http://example.com/big/index.html
PASSED URL 1 = newindex.html
OUTPUT = http://example.com/big/newindex.html
PASSED URL 2 = http://mysite.com/big/newindex.html
OUTPUT = http://mysite.com/big/newindex.html
So the simplest would be something like
var loc = location.href;
var dir = loc.substring(0,loc.lastIndexOf('/'));
function getHref(urlString) {
if (urlString) return (urlString.toLowerCase().indexOf('http:')==0)?urlString:dir+'/'+((urlString.indexOf('/')==0)?urlString.substring(1):urlString);
}
I am using the location object, substring, indexOflink text, lastIndexOf and the ternary operator - nested
<script type="text/javascript">
var loc = location.href;
var baseurl = loc.substring(0,loc.lastIndexOf('/'));
function getoutputurl(inputurl)
{
var returnurl = '';
if (inputurl)
{
if(inputurl.toLowerCase().indexOf('http://')==0)
{
returnurl = inputurl;
}
else
{
returnurl = baseurl+'/' ;
if(inputurl.indexOf('/')==0)
{
returnurl = returnurl + inputurl.substring(1);
}
else
{
returnurl = returnurl + inputurl;
}
}
}
return returnurl;
}
alert(getoutputurl('http://google.com'));
alert(getoutputurl('google.com'));
</script>
Try out this code it works
Use regular expresion to check if passed url have non relative component. If not create new output url based on part of current url ( cuted via regular exp also for example) and relative part.