JSONP read text from exernal file - html

Please help
I have external link with JSON object's , link: http://creativescream.com/html5ws/api/news.
Now i must to read json on my site but i can;t do that. I used to make my json file and that work ok but that is not a poent i must read JSON from external link.
I tried this code but it will not work:
<script>
$.getJSON("http://creativescream.com/html5ws/api/news",
function(json){
for(i=0; i<3; i++){
var newsTitle = json.contents[i].title;
var newsContent = json.contents[i].news;
var newsAuthor = json.contents[i].author;
var newsLink = json.contents[i].url;
$("#latest_news").append("<h2>" + newsTitle + "</h2><br />");
$("#latest_news").append("<p>" + newsContent + "</p><br /><a href='" + newsLink + "' target='_blank'>[Pročitaj opširnije]</a><br />");
$("#latest_news").append("<span style='color: #000000; font-weight: bold;margin-left:10px; '>Author: " + newsAuthor + "</span><br /><br />");
}
});
</script>
Please help me.

The site isn't returning jsonp nor is jquery treating it as such. Add ?callback=? to the end of the URL in order for jquery to treat it like jsonp and tell the server the function name to inject into the response.

Related

Trying to restart my Twitch API from scratch. Not sure how to get my response

I'm in the process of redesigning my website, and with this I wanted to update/improve the various scripts involved. One such script is a call to the Twitch API for a set group of streamers, then displaying who's online and, if any are, displaying the first live stream and allow the user to switch between them.
My original script, which works great, was originally constructed by somebody here on StackOverflow. The problem is that instead of guiding me to a usable answer, they just rewrote my code to work in a way that I have a hard time following. I honestly have no idea how the loop they made works. It works, and I use it now, but that's my mistake. I want to start fresh and get a return for each user in an array, then construct things out based on the results of their live status. I don't understand how it is currently being done with this code, and I can't seem to figure out the point where it is saving the return, so I can't properly log it to the console to look at it and use it proper.
Long story short, can anybody help me with a simple return in a loop for each user so I can log it to the console and look at it and use it DIRECTLY instead of saving pieces to a million variables? Here's the code I'm currently using:
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
var reqs = $.map(casters, function (userName, i) {
return $.getJSON('https://api.twitch.tv/kraken/streams/' + userName + '.json?callback=?');
})
$.when.apply($, reqs).done(function () {
$.each(arguments, function (i, array) {
var data = array[0];
var userName = casters[i];
console.log(data);
if (data.stream === null) {
$('<div class="streampost"></div>').append(
'<a id="offline' + userName + '" class = "offline" href="http://www.twitch.tv/' + userName + '/profile" title="' + userName + '" target="_blank"></a>').appendTo('#statusblock');
}
else {
castersLive.push(casters[i]);
$('<div class="streampost"></div>').append(
'<a id="online' + userName + '" class = "online" href="http://player.twitch.tv/?channel=' + userName + '" title="' + userName + '" target="stream"></a>').appendTo('#statusblock');
}
});
console.log(castersLive);
if (castersLive.length > 0) {
$('<div class="streamplayer embed-responsive embed-responsive-16by9"></div>').append('<iframe src="http://player.twitch.tv/?channel=' + castersLive[0] +'" name="stream"></iframe><iframe src="http://www.twitch.tv/' + castersLive[0] + '/chat?popout=" frameborder="0" scrolling="no" height="478" width="350" id="chatFrame" name="chatFrame"></iframe>').appendTo('#streamblock');
}
else {}
$('.online').click(function() {
var streamName = $(this).attr('title');
$('#chatFrame').attr('src', 'http://www.twitch.tv/' + streamName + '/chat?popout=');
});
});
});
And here is where I'm trying to restart, but I can't seem to get the call to the API correct. I'm still a bit new to all this, and I also use the Tumblr and YouTube APIs. Each is different, and it throws me off a little.
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
for (i=0; i <= casters.length; i++) {
$.get('https://api.twitch.tv/kraken/streams/' + casters[i] + '.json?callback=?'), function ( data ) {
console.log(data);
};
};

Add Extension to Index File

I've been searching online for an answer to this but I can't seem to find anything which can help.
I was wondering if its possible to open a index.html file with an extension after the .html
(for example it would open a file like this - index.html?lc=uk) automatically when you would double click the file or when you click on a link which connects to that file.
Hope that makes sense.
If anyone could help would be much appreciated.
Regards,
Seb
user2072826
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName));
var suffix = url.substring(url.indexOf(paramName));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
and then in the body tag:
<body onload="setGetParameter('lc', 'uk');">
This has worked but the problem is that it keeps refreshing the page constantly. Is there a way to stop the refreshing?
Unfortunately this does not work. You can not pass URL Parameters into the file-name.
If you want to add it as the page loads, you could add this to the JavaScript of the page:
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if(!(url.indexOf(paramName) >= 0))
{
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName));
var suffix = url.substring(url.indexOf(paramName));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
}
and then in the body tag:
<body onload="setGetParameter('lc', 'uk');">
Original Source(Yes, there is a difference between the code)
If you are using Apache, you may use mod_rewrite to perform almost any kind of "magic" with request file names. As long as you know home to use regular ecpressions, of course. I suggest you to do some research on mod_rewrite.

Query String - Multiple parameters

I am trying to pass the following Query String but am being met with a Webpage not available error:
window.location.href = "chaindeals.html?chainID=" + chainID + "&imageURL=" + imageURL;
I also tried changing the & to & but I'm getting the same error.
Would anyone know what I am doing wrong?
Thanks for your help.
please try this.
var url = '';
url = url.concat('chaindeals.html?chainID=',chainID, '&imageURL=', imageURL);
window.location.href = url;
Try to give the full url to window.location.href, not only the page name :
var host = "http://" + window.location.host;
window.location.href =
host
+ "/chaindeals.html"
+ "?chainID=" + chainID
+ "&imageURL=" + imageURL;

Embed a video using Google Script

I'm trying to use Google Scripting to embed videos I have stored on my Google Drive into HTML. Here's the code I'm using to generate the HTML:
function GenerateTables() {
var folderz = DocsList.getFolder('SharedVideos');
var contents = folderz.getFiles().sort(function(a,b) {return b.getDateCreated()-a.getDateCreated()});
var file;
var name;
var date;
var url;
var dateModified;
var folder;
var textOutput = "<table>";
textOutput += "<tr><th>File Name</th><th>Create Date</th><th>Modified Date</th></tr>"
for (var i = 0; i < contents.length; i++) {
file = contents[i];
folder = file.getParents()[0];
name = file.getName();
url = "https://docs.google.com/file/d/" +file.getId()+"/preview";
date = Utilities.formatDate(file.getDateCreated(), 'GMT-6', 'MM/dd/yyyy hh:mm:ss aaa');
dateModified = Utilities.formatDate(file.getLastUpdated(), 'GMT-6', 'MM/dd/yyyy hh:mm:ss aaa');
textOutput += "<tr><td>" +name + "</td><td>" + date + "</td><td>" + dateModified + "</td></tr>";
textOutput += "<tr><td colspan='3'>";
textOutput += "<iframe src='"+url+"' height='385' width='640'></iframe>";
textOutput += "</td></tr>";
}
textOutput +="</table>";
return textOutput;
}
function doGet() {
var text = GenerateTables();
return HtmlService.createHtmlOutput(text);
}
The iframe is being converted to
<iframe data-caja-src='[the correct URL]' height="385" width="640"></iframe>
I was hoping it would be a simple process, but the more I got looking into it, the more complicated it seemed to be. Is there an easier way to do what I'm trying to do? I don't have much experience using Caja, but maybe there's a simple way to embed a video using Caja, I just couldn't get anything to work.
There is not yet a way to embed videos inside HtmlService. I think there's a feature request for it on the issue tracker, but regardless it's something we know about.

Pass parameter to another page to generate listview from Json

Hello I'm using this js to pass the url parameter and it's working just fine, but my problem is that when I define the path to the JSON file I don't want to use the id of the item...I want to use another Id. For example: I have the following item:
{"id":"1",
"name":"Winery",
"street":"Chile",
"number":"898",
"phone":"4204040",
"mail":"winery#hotmail.com",
"web":"www.winery.com",
"lat":"-32.891638",
"long":"-68.846522",
"id_localidad":"1",
"id_provincia":"1"}
I want to put id_localidad at the end of the path, to generate the listview depending on the city (id_localidad is the id of the city where the shop is), not the id of the item. And this is not working for me.
Thanks in advance!
JS FILE
$('#PuntosDeVenta').live('pageshow',function(event){
var id = getUrlVars()["id"];
$.getJSON('http://localhost/CavaOnline/json_PuntosDeVentas.php?id_localidad='+id, function(vinerias) {
//THIS IS NOT WORKING, IS THE SAME AS PUTTING id, not id_localidad
$.each(vinerias, function(index, vineria) {
$('#listviewVinerias').append( '<li><a href="FichaTecnicaVineria.php?id=' + vineria[id - 1].id + '" > ' +
'<img src="pics/' + vineria[id - 1].img_url1 + '"/>' +
'<h4>' + vineria[id - 1].name+'</h4>' +
'<p>' + vineria[id - 1].street+ ' ' + vineria[id - 1].number+ '</p>' +
'</a></li>');
$('#listviewVinerias').listview('refresh')
});
});
});
function getUrlVars() {
var vars = [], hash;
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;
}
Div where I load the List
<div data-role="content">
<ul id="listviewVinerias" data-role="listview"></ul>
</div>
So I'm assuming your vinerias is a variable containing a list of JSON objects, even though I don't know why you are calling [id-1] everywhere.
If so, you can use the .filter() function to filter out the elements that have an id_localidad equal to the one specified.
var filteredVinerias = vinerias.filter(function(index){
return this["id_localidad"] === "1" //The localidad you want
});