In my view, I have a javascript function to handle a select event on a pie chart. The function is shown below:
function selectHandler() {
var selectedItem = visualization.getSelection()[0];
if (selectedItem) {
var val = data.getFormattedValue(selectedItem.row, 0);
location.href = '/Tickets';
}
}
Currently I am on the Home Controller in the Groups View. I want to navigate to the Index View of the Tickets Controller while passing the selected value from the javascript variable "val". How would I go about doing this?
Are you intending to manually navigate the user?
If you're looking for a redirect JavaScript way, then you would do something as simple as...
location.href = '/Tickets?value=' + val;
Now this may not work for everything. For example, if location.href already contains a '?', and you need to maintain that context, then you need to use '&'. Maybe your app lives in a Virtual Directory.
You might do something like...
var newUrl = location.href;
if (newUrl.indexOf('?') > -1)
newUrl += '&';
else
newUrl += '?';
newUrl += val;
This allows you maintain any existing context as well.
If you expect the ticket to already be defined, you might need to remove that from the query string, if it already exists.
In that case then you might want to do something like...
var params = location.search.substring(1).split('&'),
paramToRemove, indexOfValue,
hasSearch = false,
param;
for (var i = 0, len = i; i < len; i++)
{
param = params[i];
indexOfValue = param.indexOf('value');
hasSearch = param.indexOf('?') === 0;
if (indexOfValue === 0 || (indexOfValue === 1 && hasSearch ))
{
paramToRemove = params[i];
break;
}
}
var newUrl = location.href;
// Remove old value
if (paramToRemove) newUrl = newUrl.replace(paramToRemove, hasSearch ? '?' : '');
// Add proper search char
if (newUrl.indexOf('?') > -1)
newUrl += '&';
else
newUrl += '?';
// Add new value
newUrl += val;
location.href = '/Tickets?val=' + val;
//On page load the server will generate the URL for you.
var ticketURL = '#Url.Action("Index", "Tickets")';
//Append the value to the URL
ticketURL = ticketURL + '?val=' + val;
//Do your stuff!
Since, you are calling Controller methods from javascript. You should make an POST ajax call to Ticket Controller and passing Action method name also.
Your code would be like this:
return $.post('/Ticket(ControllerName)/Index(method name)/',parameters here);
Inside API Controller, Index method will accept the same param which we are passing from our javascript.
ActionResult Index(parameter){...}
Related
I'm using a script to add parameters to a URL to use with a if statement for a layout change. It works with a structure like this:
/Computers/Desktops
on click to change layout:
/Computers/Desktops?param=list
and
/Computers/Desktops?param=grid
I'm using a filer that adds parameter, then the layout will change between something like this:
/Computers/Desktops?param=list&fss=HP+2GB
and on click:
/Computers/Desktops?param=grid&fss=HP+2GB
This works well, but I need to make this work for another structure that looks like this:
/Computers/Desktops/Apple/Apple-iMac-Core-i5-2-7-GHz-8-GB-1-TB-LED-2?prodid=30811
Here I want to add the parameter before ?prodid=30811 so the layout changes on click between
/Desktops/Apple/Apple-iMac-Core-i5-2-7-GHz-8-GB-1-TB-LED-2?param=list&prodid=30811
/Desktops/Apple/Apple-iMac-Core-i5-2-7-GHz-8-GB-1-TB-LED-2?param=grid&prodid=30811
I hoped I just could change FSS in my script to prodid, but this doesn't work. So why doesn't it work and why, and how do I do this instead? My if/else script already works, I just need the correct URL from the buttonclick.
$('.click3').on('click', function() {
console.log("Clicked");
var baseUrl = window.location.href.split("?")[0];
var fss = getParametersByName("fss");
var params = getParametersByName("fss");
if (params == "list")
param = "grid";
else
param = "list";
var newUrl = baseUrl + "?param=" + param;
if ((fss).length > 0)
newUrl = newUrl + "&fss=" + fss;
window.location.href = newUrl;
function getParametersByName(name) {
name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
});
using MVC.
In one of my controllers I am creating a cookie like the following:
Function Login(ByVal access_token As String) As ActionResult
Response.Cookies.Add(New HttpCookie("access_token", access_token))
End Function
I want to check the value of the cookie called "access_token" client-side, in order to know the next step to take in javascript.
How can I do that?
Any help would be appreciated
UPDATE
I found the solution:
I just add this function in Javascript :
function ReadCookie(cookieName) {
var theCookie = "" + document.cookie;
var ind = theCookie.indexOf(cookieName);
if (ind == -1 || cookieName == "") return "";
var ind1 = theCookie.indexOf(';', ind);
if (ind1 == -1) ind1 = theCookie.length;
return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}
and I call it on Document.ready() with the name of the cookie that I want to retrieve like that:
if (ReadCookie("access_token") != '')
{ }
I tried to use withParameters method on query like that:
query.withParameters({ includeLocation: true })
Unfortunately my parameter was not added to url. I use breeze.debug.js and I've found this line in it
//queryOptions = __extend(queryOptions, this.parameters);
Is that a bug ? Is withParameters support taken out ? Or do I do something wrong ?
I use oData
When .withParameters is used, the parameters are added to the URL by the data service adapter, not by the Breeze core. That's why that line is commented out. This allows the parameters to be encoded differently, depending upon the backend that is used.
That's great, but the data service adapter for OData that ships with Breeze 1.4.8 does not handle .withParameters. The WebApi adapter does, but not the OData adapter. We'll make sure it's added in a future release. In the meantime, you can continue to use your workaround.
This oversight/omission is partly because we don't know any OData services that handle custom parameters. If I may ask, what OData service are you using?
It looks like this will hopefully be fixed soon: https://github.com/Breeze/breeze.js/issues/19.
In the meantime, you can use this code as a workaround (kudos to the author of this pull request):
var odataAdapter = breeze.config.getAdapterInstance('uriBuilder', 'OData');
var origBuildUri = odataAdapter.buildUri;
odataAdapter.buildUri = function (entityQuery, metadataStore) {
var uri = origBuildUri(entityQuery, metadataStore);
//Add custom query option support to webapi odata.
//See https://github.com/Breeze/breeze.js/issues/19
if (entityQuery.parameters !== null && typeof entityQuery.parameters === 'object'
&& Object.keys(entityQuery.parameters).length)
{
var queryParams = {};
for (var param in entityQuery.parameters) {
if (/^([^\$].*)$/.test(param)) {
var val = entityQuery.parameters[param];
if (typeof val == 'string') val = "'" + val + "'";
queryParams[param] = val;
}
}
//get the uri without the resourceName
var resourceName = entityQuery.resourceName;
uri = uri.substr(resourceName.length + 1);
//build the new uri
uri = resourceName + toQueryOptionsString(queryParams) + '&' + uri;
}
//Copied from breeze.js OData adapter
function toQueryOptionsString(queryOptions) {
var qoStrings = [];
for (var qoName in queryOptions) {
var qoValue = queryOptions[qoName];
if (qoValue !== undefined) {
if (qoValue instanceof Array) {
qoValue.forEach(function (qov) {
qoStrings.push(qoName + "=" + encodeURIComponent(qov));
});
} else {
qoStrings.push(qoName + "=" + encodeURIComponent(qoValue));
}
}
}
if (qoStrings.length > 0) {
return "?" + qoStrings.join("&");
} else {
return "";
}
}
return uri;
};
I'm looking for a way to access the Query String of a page that has been loaded into a div, through jquery or javascript.
The reason is that I need to modify how the page calls a function based on whether a parameter has been loaded into the page or not.
I've tried this inside the page that has been loaded into the div
var fileIDPresent = false;
var url = this.window.location.href;
alert(url);
if (url.indexOf('?' + 'FileID' + '=') != -1)
fileIDPresent = true;
else if (url.indexOf('&' + 'FileID' + '=') != -1)
fileIDPresent = true;
The page is loaded into the div through the following code:
$('#loadedContentHolder').load('/ViewDetails.aspx?FileID=' + File, function () {
// load finished...
$('#loadedContentHolder').slideDown('slow');
});
The URL is lost when you inject the code into the DIV. You need to save the URL to the div node somehow.
You could do this:
var url = '/ViewDetails.aspx?FileID=' + File;
$('#loadedContentHolder').load(url, function () {
// load finished...
$('#loadedContentHolder').data('url', url).slideDown('slow');
});
Next time you want to know what URL was used you can fetch the url like this:
var url = $('#loadedContentHolder').data('url');
This will return the URL that was used and you can use that url to extract the params:
var params = getUrlParams(url);
function getUrlParams(url) {
var vars = [], hash
,hashes = url.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
if (hash.length === 2) {
vars.push(hash[0]);
vars[hash[0]] = unescape(hash[1]);
}
}
return vars;
}
I've got a dropdown menu on my form, which when something is selected I need to reload the current page, but with an appended querystring.
How would I go about doing this?
This is an old question but it came up first in google search results.
The solution I went with is similar to jAndy's.
window.location.pathname gives me the page's url without the query string.
I'm then able to build the query string with "?"+$.param({'foo':'bar','base':'ball'}) which I then append to the pathname and set to window.location.href.
window.location.href = window.location.pathname+"?"+$.param({'foo':'bar','base':'ball'})
var params = [
"foo=bar",
"base=ball"
];
window.location.href =
"http://" +
window.location.host +
window.location.pathname +
'?' + params.join('&');
That code within your change event handler will do the trick.
For instance:
$('#my_dropdown_id').bind('change', function(){
var params = [
"foo=bar",
"base=" + $(this).val()
];
window.location.href = "http://" + window.location.host + window.location.pathname + '?' + params.join('&');
});
If you go with the top rated answer, you may want to replace
http://
in the code with
window.location.protocol
so that it works for other protocols, like https or file. So
window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + params.join('&');
Actually, there a built-in function of location that you can use, the name of the function is assign.
For appending or modifying there is another built-in function of the URL class that you can use too. the name of the function is searchParams.
So for your case you just need below example:
const url = new URL(location.href);
url.searchParams.set('key', 'value');
location.assign(url.search);
Update 2022
I create a TypeScript function to apply redirect with params more easier:
const isClient = (): boolean => typeof window !== 'undefined';
type ParamsType = { [key: string]: string | number };
const redirectUrl = (url: string, params?: ParamsType): void => {
if (isClient()) {
try {
const _url = new URL(url);
if (params) {
const keyList = Object.keys(params);
for (let i = 0; i < keyList.length; i += 1) {
const key = keyList[i];
_url.searchParams.set(keyList[i], params[key]?.toString());
}
}
window.location.assign(_url.href);
} catch (e) {
throw new Error('The URL is not valid');
}
}
};
export default redirectUrl;
If you want a simple way to preserve the query string and possibly append to it, use window.location.search; here's a snippet:
var search = window.location.search + (window.location.search ? "&" : "?");
search += "param1=foo¶m2=bar";
window.location.href = window.location.protocol + "//" + window.location.host + window.location.pathname + search;
You can, of course, use a more sophisticated way of building the rest of your query string, as found in the other examples, but the key is to leverage Location.search.
If you have an existing querystring that you'd like to keep then this version does that and adds your new params to any existing ones. The keys are converted to lowercase so that duplicates are not added. Maintaining the quersytring does make the solution more complicated, so I'd only do this if you need to.
$("#sortby").change(function () {
var queryString = getQueryStrings();
// Add new params to the querystring dictionary
queryString["sortby"] = $("#sortby").val();
window.location.href =
window.location.protocol + "//" +
window.location.host +
window.location.pathname +
createQueryString(queryString);
});
// http://stackoverflow.com/questions/2907482
// Gets Querystring from window.location and converts all keys to lowercase
function getQueryStrings() {
var assoc = {};
var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
var queryString = location.search.substring(1);
var keyValues = queryString.split('&');
for (var i in keyValues) {
var key = keyValues[i].split('=');
if (key.length > 1) {
assoc[decode(key[0]).toLowerCase()] = decode(key[1]);
}
}
return assoc;
}
function createQueryString(queryDict) {
var queryStringBits = [];
for (var key in queryDict) {
if (queryDict.hasOwnProperty(key)) {
queryStringBits.push(key + "=" + queryDict[key]);
}
}
return queryStringBits.length > 0
? "?" + queryStringBits.join("&")
: "";
}
I was having a requirement to open a particular tab after reloading. So I just needed to append the #tabs-4 to the current url. I know its irrelevant to current post but it could help others who come to this just like I did.
Using the code
window.location = window.location.pathname
+ window.location.search + '#tabs-4';
did'nt work for me but below code did.
location = "#tabs-4";
location.reload(true);