Url
/product/product/4415/category/44/display/1/
var url = location.href;
var getAr0 = url.indexOf("44");
if(getAr0 != -1) {
$("#asideNav-top").addClass("open");
}
Try to execute the if statement only when /category/44/.
The problem is when the URL is
Url
/product/product/4415/category/71/display/1/
//Even if the above statement is executed.
if(getAr0 != -1) {
$("#asideNav-top").addClass("open");
}
What I want is to run the if statement only when /category/44/.
You need to find /category/44/ but not just 44 like below;
var url = location.href;
var getAr0 = url.indexOf('/category/44/');
if(getAr0 != -1) {
$("#asideNav-top").addClass("open");
}
Related
I'm trying to get post id of image and then get specific size of image.
If I give hardcode single image url then it works fine but if I give url through variable then it returns 0.
It is strange behaviour
here is function
function get_image_id_and_size($image_url)
{
//return $image_url;
$image_id = attachment_url_to_postid($image_url);
return $image_id;
$image_large = wp_get_attachment_image_src($image_id, 'large');
return $image_large[0];
}
and here is javascript code
var picture = document.querySelectorAll(".active");
for (var i = 0; i < picture.length; i++)
{
var src = picture[i].src;
if (typeof src !== 'undefined')
{
var res;
if (src.includes('bedbase') && src.includes('12in_'))
{
var imgurl = src.split('12in_');
var imgres = imgurl[1].split('-');
var orgurl = imgurl[0] + "12in_" + currentcolor + ".png";
//console.log(orgurl);
res = <?php echo get_image_id_and_size(orgurl); ?>;
console.log(res);
//picture[i].src = res;
}
}
}
I checked if I return the urls it works, as you can see I commented that return line. But it don't get the post id it returns 0.
But if I give hardcode single link in argument then it works and returns post id.
I had a similar problem where the domain did not have a www. but the file url did, and that would return a 0.
I want to check if there is a hash in the url :
This should return true:
domain.com/balbla#hash
This should return false:
domain.com/balbla
So I use URI.js to grab the hash
var uriFragment = new URI(window.location.href).fragment();
if (uriFragment.length) {
console.log('yep')
} else {
console.log('nope')
}
But that never returns nope even if there is no hash in the url.
Why not using directly location.hash
if(location.hash.length > 0) {
console.log('yep')
}else {
console.log('nop')
}
Below code will give the list of hash tags in array format.
var hash = window.location.hash;
return result = hash.split('#').reduce(function (result, item) {
var parts = item.split('=');
result[parts[0]] = parts[1];
return result;
}, {});
i'm checking to see if pages are in array currently when clicking on hsc.html and index.html i'm still seeing the alert("debug") which is == to requested page not !=
experimentPages = new Array();
experimentPages[1] = "index.html"
experimentPages[2] = "hsc.html"
$('a').click(function () {
var reqestedPage = $(this).attr("href");
if ($.inArray(experimentPages) != reqestedPage) {
alert("debug")
}
The method works as follows
$.inArray( value, array_to_check )
and returns -1 if not found, otherwise it returns the position of the value in the array.
var experimentPages = [
"index.html",
"hsc.html"
];
$('a').click(function () {
var reqestedPage = $(this).attr("href");
if ($.inArray(reqestedPage, experimentPages) != -1) {
alert("it exists");
}else{
alert("no luck");
}
});
note that the href has to match exactly the values in the array
I am using local storage variable to hold the location of a users current progress. I have ran into a problem whereby if the last section that the user was on has been since deleted I am getting a target invocation must be set error. This is my code:
if (localStorage["Course" + '#Model.Course.CourseID'] != null && localStorage["Course" + '#Model.Course.CourseID'] != "") {
var id = localStorage["Course" + '#Model.Course.CourseID'];
}
else {
var id = '#Model.CourseSections.First().CourseSectionID';
}
I need to check using javascript that the localStorage course section is still existing in the database so I created the following ViewModel method:
public bool CourseSectionLaunchStillExistCheck(int courseSectionID)
{
this.TargetCourseSection = courseSectionRepository.Get(cs => cs.CourseSectionID == courseSectionID).FirstOrDefault();
if (this.TargetCourseSection != null)
{
return true;
}
else
{
return false;
}
}
But when I try to use the following javascript:
if (localStorage["Course" + '#Model.Course.CourseID'] != null && localStorage["Course" + '#Model.Course.CourseID'] != "") {
var id = localStorage["Course" + '#Model.Course.CourseID'];
if ('#Model.CourseSectionLaunchStillExistCheck(id)' != true) {
var id = '#Model.CourseSections.First().CourseSectionID';
}
}
else {
var id = '#Model.CourseSections.First().CourseSectionID';
}
It is failing to recognise the id parameter saying it does not exist in the current context. How can I ensure that the course section exists using javascript before setting the variable?
Could I use a post such as:
var postData = { 'courseSectionID': id };
$.post('/Course/CourseSectionLaunchStillExistCheck/', postData, function (data) {
});
and then how could i check if the result of this post data would be true or false?
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){...}