Trying to add variables to twitter json url - javascript

I'm trying to create a function which uses a combination of jquery, json and javascript to retrieve a twitter feed using the $.getJSON() method.
I have the function working with a static url like so:
$.getJSON("http://twitter.com/status/user_timeline/owzzz.json?count=1&callback=?", function(data) {
What I'm trying to do is replace where you see the username owzzz and count with values passed into the function.
the function looks something like this:
var twitterFeed = function(username, count){
$.getJSON("http://twitter.com/status/user_timeline/owzzz.json?count=1&callback=?", function(data) {
$.each(data, function(i) {
var timestamp = new Date(this.created_at);
var text = this.text;
$("#twitter").html(text +'<a href="http://twitter.com/' + username + '/" class="timestamp">' + username + ' <span>' + timestamp.toDateString() + '<\/span><\/div>' ).click(function(e) {
window.location = 'http://twitter.com/' + username;
e.preventDefault();
});
});
}); } twitterFeed('owzzz',1);
I can add the passed value username in the .html() but not inside the getJSON();
Any idea how I would go about this?

Do the same as you do in the html function: String concatenation.
$.getJSON("http://twitter.com/status/user_timeline/" + username + ".json?count=" + count + "&callback=?", ...)

string concatenation.
$.getJSON(
'http://twitter.com/status/user_timeline/' + username + '.json' +
'?count=' + count + '&callback=?'
);

Related

Passing parameters into a url

I need to pass some parameters into a URL
For example, my URL is mypage/generator/
So my generator consists of a generator(code, id, text)
Is the following a correct way of doing it
Check:function () {
var generator = code + "," + id + ", " + text;
Window.location.href = "mypage/" + generator + "/"
}
You can try this:
function check(){
var generator=code +","+ id +","+ text;
window.location.href="mypage?data="+generator+"";
}
and you can access the data from request.getparameter("data");
This might help you!

Passing a string with quotes inside of it from API response as argument to a function in JS

I'm passing below data that i'm capturing from an API to the function addToList().
//...
onclick="addToList(\'' + value.id + '\',\'' + value.title + '\',\'' value.image_url + '\')"
//...
function addTolist (id, title, image_url)
{
var data = {};
data.id = id;
data.title = title;
data.image_url = image_url;
data.format = "json";
$.ajax({
//...
}
My problem is that certain titles do have quotes in it (e.g A Knight's Tale) and i'm getting a syntax error because of it.
Is there a way to change the value that I'm capturing from value.title in order for it to be accepted as argument to my addToList function?
Thanks

jQuery get an Array from json file

I need to get an array from a json file and I have no idea how to do it.
Here's the code I want to get the array from:
$.getJSON('saveGames/userID' + userID + '_SAVEALPHA.json', function(data) {
console.log("Save data from: userID" + userID + "_SAVEALPHA.json: " + data);
var testVar = jQuery.parseJSON(data);
var pokemonAryTest = testVar[0].pokemon;
pokemonAry = [pokemonAryTest];
console.log("loaded players Pokemon: " + pokemonAry);
console.log(pokemonAry[0])
});
I have tried to change the index of pokemonAry to 1 and it returns undefined. And when I keep the index the same, it returns ["Pikachu, Charmander"] so I think its acting like as if it's a string.
Here's the .json:
"[{\"userID\":\"1\",\"saveName\":\"g\",\"pokemon\":[\"Pikachu, Charmander\"]}]"
When you are using getJSON() method no need to parse JSON data, jQuery take care of it. :)
$.getJSON('saveGames/userID' + userID + '_SAVEALPHA.json', function(data) {
console.log("Save data from: userID" + userID + "_SAVEALPHA.json: " + data);
var pokemonAry = data[0].pokemon;
console.log("loaded players Pokemon: " + pokemonAry);
console.log(pokemonAry[0])
});

pass two parameters DDL in JavaScript / jQuery

This function works fine but passes only one parameter from one DropDownList to the controller. How to make the transfer of two parameters of an (two DDL)?
$(function () {
$('#DDL_1_ID').change(function () {
var URL = $('#FormID').data('someAction');
$.getJSON(URL + '/' + $('#DDL_1_ID').val(), function (data) {
$.each(data, function (i, format) { });
});
});
});
You could continue appending query string parameters as you've done with ddl_1_tc...
$.getJSON(URL + '/' + $('#DDL_1_ID').val() + '&myotherparam=' + yourotherinput
Your url appears to be malformed though. I believe you're missing the name of the first value...
$.getJSON(URL + '?nameforfirstparam=' + $('#DDL_1_ID').val() + '&nameforsecondparam=' + yoursecondinput

Showing local storage values on HTML page

I have a survey that stores values to local storage. Trying to make a very simple 'admin' page that its sole purpose is to only display the local storage in an semi organized fashion via table etc. I'm not exactly sure how to go about it.
Here is what i have to store it.
$( document ).ready(function() {
$('#Save').click(function (e) {
if (confirm('Are you sure you want to submit? You will not be able to go back.')) {
var person = $("#FirstName").val() + "." + $('#LastName').val();
$('input, select, textarea').each(function () {
var value = $(this).val(),
name = $(this).attr('name');
localStorage[person + "." + name] = value;
window.location.href = "Confirmation.html";
console.log('stored key: ' + name + ' stored value: ' + value);
});
}
});
});
the key stored is firstname.lastname.element,value. if it could simply display basically like the console view ordered by key that would be great. Any point in a direction would be appreciated.
Unless you really can't manipulate your server side code, I highly recommend that you use $.ajax to post your form to the server and then serve back a response. I doubt that local storage was intended for form submission in quite this way. Otherwise, see below.
In order for you to show your localStorage items on the next page, you need to reference your localStorage items on the next page. After you change window.location.href, the previous page's context is no longer valid, thus name and value no longer exist. You should use something similar to this for both pages:
Submit.html
$(document).ready(function() {
$('#Save').click(function (e) {
if (confirm('Are you sure you want to submit? You will not be able to go back.')) {
var person = $("#FirstName").val() + "." + $('#LastName').val();
$('input, select, textarea').each(function () {
var value = $(this).val(),
name = $(this).attr('name');
localStorage["name"] = name;
localStorage["person"] = person;
localStorage[person + "." + name] = value;
window.location.href = "Confirmation.html";
console.log('stored key: ' + name + ' stored value: ' + value);
});
}
});
});
Confirmation.html
$(document).ready(function() {
var name = localStorage["name"];
var person = localStorage["person"];
var value = localStorage[person + "." + name];
console.log('stored key: ' + name + ' stored value: ' + value);
});

Categories