Variable in AJAX url - javascript

How can I use a variable within an AJAX call URL?
So far I have the following:
id = $(this).children('span.title').attr('data-id');
$("#test").load("http://localhost:8888/projects/superfreerespo/ + id/ .gameBox-Ops");
There is something wrong with the way I have declared the url but being relatively knew to Ajax I am a little unsure where I am going wrong.
Just to point out the .gameBox-Ops is not part of the URL, it is the class of the container I am trying to call with AJAX

id is a variable, you have to escape the string to concatenate it into it.
$("#test").load("http://localhost:8888/projects/superfreerespo/"+id"+/ .gameBox-Ops");

$("#test").load("http://localhost:8888/projects/superfreerespo/" + id + "/ .gameBox-Ops");
Not related to AJAX, your string concatenation is wrong.

Use proper string concatenation. Also I am not sure why you have . on last part.
id = $(this).children('span.title').attr('data-id');
$("#test").load("http://localhost:8888/projects/superfreerespo/" + id + "/.gameBox-Ops");

Related

How to pass a javascript variable in an ajax url?

I have a variable as per below:
var myip;
And I want to insert it in the url below:
$.ajax('http://api.ipstack.com/**[myip]**?access_key=mykey')
If I manually put in my ip in place of [myip] it gives me the desired results but I want to automate it.
I tried with the methods given in the url below but it didn't help.
How to pass Javascript variables inside a URL? AJAX
Thanks in advance for going through and helping!
Use string template.
$.ajax(`http://api.ipstack.com/${myip}?access_key=mykey`)
^ ^^^^^^^ ^
Or using string concatenation.
$.ajax('http://api.ipstack.com/' + myip + '?access_key=mykey')

How to find an ID that contains a string if the string is held inside a variable?

This may be a very simple question in syntax but I've been googling and still can't find the answer.
I'm trying to set focus to the first SELECT element which has a specific string but the string is held in a dynamic variable. What would be the correct syntax? I thought the below would be correct but it doesn't seem to work.
$('SELECT[id*=" + qID"]').focus();
Try this
$('select[id*="' + qID + '"]').focus();

Escaping apostrophe in jQuery

I'm trying to escape out of some apostrophe's in my JSON. I know it's not ideal but i append the data to the DOM and call it later in the code. At the moment i get an error whenever there is an ' in the data. I've tried using replace and encodeURI but just doesnt seem to work.
My code basically looks like:
var addItem = function (item, target){
var obj = $(item).data('obj');
var obj_string = JSON.stringify(obj);
target.append("<div data-obj='" + obj_string + "'> Added </div>
}
When i inspect the element it breaks when it gets to the apostrophe:
{"publisher":"EA","games":[{"game":"Dragon's"}]}
Looks like this in the element inspector:
{"publisher":"EA","games":[{"game":"Dragon" s"}]}
And everything that follows is broken. Any ideas how i escape it?
I've found lots of ways if it was pureply jquery but with it being in the html it seems to not work.
You can avoid the escaping and stringification© using .data() to set the obj to the element directly.
$('<div/>').data('obj', obj).text('Added').appendTo(target);
Just keep in mind with this method you won't get a data attribute on the actual element itself but the data will be there when you request it with .data.
You are concatenating your content with a string that already uses single quotes. The concatenated result will most likely be <div data-obj='Dragon's'> which is not what you want.
Either escape the single quote when concatenating it (not sure the entity won't be interpreted):
.append("<div data-obj='" + obj_string.replace("'", "‚") + "'> Added </div>");
Or safer, you can build your nodes with jQuery which will give you a native escaping for some performance penalty:
.append($("<div>Added</div>").data("obj", obj_string));
Rather than leaving yourself open to XSS, try:
var div = document.createElement('div');
div.appendChild(document.createTextNode("Added"));
div.setAttribute("data-obj",obj_string);
target.append(div);

Javascript syntax: inserting the value of a string into a statement

My question is simple but I can't seem to find the solution. I'm changing my html through javascript. I have a header with an image that I want to change every 3 seconds or so. I made an array with the name of the images I want in my cycle.
For changing the image I made a variable with the name of the image. I then try to insert the value of the string into the follow statement:
imageParent.style.backgroundImage = "url('images/"nextImage".jpg')";
But as you see this is completely the wrong syntax. What is the correct syntax for this?
What you're trying to do is known as string concatenation. In JavaScript it is most easily done using the + operator:
"url('images/" + nextImage + ".jpg')"
See The + Operator Used on Strings at http://www.w3schools.com/js/js_operators.asp.
try maybe +nextImage+ instead of nextImage

Parse URL value and remove anything after the last '/'

My website URL is (example) http://mypage.com/en/?site=main. Then comes JavaScript code that, together with PHP, parses the data.
After that, I need some code that will change the URL inside the adress bar to http://mypage.com/en/, that is, removes the stuff after the last / (slash).
If possible, it is should be jQuery/JavaScript code.
I found something that will work.
You have to use a method called replaceState().
Mozilla developer reference
var str = window.location.href;
window.history.replaceState({}, '', str.substr(0,str.lastIndexOf("/"))+"/");
Use split() function in javascript.
Example :
var url = "http://mypage.com/en/?site=main";
alert(url.split('?')[0]);

Categories

Resources