How can i call javaScript function in iOS Swift - javascript

i am trying to get html string from a web view. i try to get html string from web view like,
let html = webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
print(html)
in this code i get all Html code without the input value what i given after web loading.
on the other hand there is function which that will give me full html code with input value. but i was unable to call javascript function in my swift code.also my javaScript function have a parameter which one i want to call.
i was try to call javaSript function like this,
let pp = webView.stringByEvaluatingJavaScript(from:"draft")
But its not working. i want to get all of html including input value.
Here is a Screenshot how it's done in android.
i was attach all of my html code here
<!DOCTYPE html>
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html charset=UTF-8'/>
<link type='text/css' rel='stylesheet' href='file:///android_asset/bootstrap.min.css'>
<script src="file:///android_asset/jquery.min.js"></script>
<title>Quick Inspection</title>
<style>
textarea{
resize: none;
}
</style>
</head>
<body style='background: #fff3d6'>
<div class='container-fluid' style='padding-top: 10px; padding-left: 0px; padding-right: 0px; background: #fff3d6'>
<div class='container'>
<div class='row'>
<form id='rendered-form'>
<div class='rendered-form'>
<div class='fb-textarea form-group field-textarea-1505524302868'>
<label for='textarea-1505524302868' class='fb-textarea-label'>1. Description of Work</label>
<textarea class='form-control textarea' name='textarea-1505524302868' id='textarea-1505524302868'></textarea>
</div>
<div class='fb-textarea form-group field-textarea-1505524384468'>
<label for='textarea-1505524384468' class='fb-textarea-label'>2. Work Status</label>
<textarea class='form-control textarea' name='textarea-1505524384468' id='textarea-1505524384468'></textarea>
</div>
<div class='fb-textarea form-group field-textarea-1505524405168'>
<label for='textarea-1505524405168' class='fb-textarea-label'>3. Progress</label>
<textarea class='form-control textarea' name='textarea-1505524405168' id='textarea-1505524405168'></textarea>
</div>
<div class='fb-textarea form-group field-textarea-1505524418785'>
<label for='textarea-1505524418785' class='fb-textarea-label'>4. Observation</label>
<textarea class='form-control textarea' name='textarea-1505524418785' id='textarea-1505524418785'></textarea>
</div>
<div class='fb-textarea form-group field-textarea-1505524433458'>
<label for='textarea-1505524433458' class='fb-textarea-label'>5. Recommendation</label>
<textarea class='form-control textarea' name='textarea-1505524433458' id='textarea-1505524433458'></textarea>
</div>
</div>
</form>
</div>
</div>
<div class="clearfix"></div>
</div>
<div id="jsonvalue" style="visibility: hidden; display:inline;"></div>
</body>
<script>
var getJsonData = $("#jsonvalue").text();
if(getJsonData != ""){
SetJsonValue();
}
function getHtml()
{
var Alldata = $('form').serializeArray();
var JsonString = JSON.stringify(Alldata);
$("div#jsonvalue").text(JsonString);
var allCode = document.documentElement.outerHTML;
return allCode;
}
function SetJsonValue()
{
var getJsonData = $("#jsonvalue").text();
var obj = $.parseJSON(getJsonData), dataObj = {};
len = obj.length;
for (i = 0; i < len; i++) {
var f_name = obj[i].name;
var f_value = obj[i].value;
var result = f_name.split('-');
var type = result[0];
if (type == 'textarea') {
$("#rendered-form " + type + "[name = " + f_name + "]").text(f_value);
} else if (type == 'text') {
$("input[name = " + f_name + "]").val(f_value);
} else if (type == 'radio') {
$('#rendered-form input[name=' + f_name + '][value=' + f_value + ']').prop("checked", true);
} else if (type == 'select') {
$("#rendered-form " + type + "[name = " + f_name + "]").val(f_value);
} else {
$(":checkbox[value=" + f_value + "]").prop("checked","true");
}
}
}
function draft(){
var getAllHtmlCode = getHtml();
Android.fullCode(getAllHtmlCode);
}
function finalsave(){
var obj = $('form').serializeArray();
len = obj.length;
//$( ".fb-radio-group").hide();
for (i = 0; i < len; i++) {
var f_name = obj[i].name;
var f_value = obj[i].value;
var result = f_name.split('-');
var type = result[0];
if (type == 'textarea') {
//if( f_value == "") {
// $( ".field-textarea-"+result[1]).hide();
//}else{
var bodyText = f_value;
var body = $("<div class='ans'> </div>");
body.text(bodyText);
$("#rendered-form " + type + "[name = " + f_name + "]").replaceWith(body);
//}
} else if (type == 'text') {
//if( f_value == "") {
// $( ".field-text-"+result[1]).hide();
//}else{
var bodyText = f_value;
var body = $("<div class='ans'> </div>");
body.text(bodyText);
$("input[name = " + f_name + "]").replaceWith(body);
//}
} else if (type == 'radio') {
//$(".l_header").hide();
//$(".field-radio-group-"+result[2]).prev().show();
//$(".field-radio-group-"+result[2]).show();
var r_value = $('#rendered-form input[name=' + f_name + ']:checked').val();
$('#rendered-form input[name=' + f_name + ']').parent().hide();
$("label[for='"+f_name+"']").append('<div class="ans">'+r_value+'</div>');
} else if (type == 'select') {
if( f_value == "" || f_value == "0") {
//$( ".field-select-"+result[1]).hide();
}else{
var lbl = $("#rendered-form " + type + "[name = " + f_name + "]").find('option:selected').text();
var body = $("<div class='ans'> </div>");
body.text(lbl);
$("#rendered-form " + type + "[name = " + f_name + "]").replaceWith(body);
}
} else {
$(":checkbox[value=" + f_value + "]").prop("checked","true");
}
}
var allCode = document.getElementById("rendered-form").outerHTML;
Android.finalCode(allCode);
}
</script>
</html>

Related

Messages disappear once they have been sent. How can I make them stay?

I have code for a messaging app here that uses js and php. The messages are sending and saving in the database properly, but they only appear on the screen for about a second and then disappear. I do not understand why this is happening because the messages are present in the table in my database. Could someone help me out?
index1.php
</head>
<body onload="checkcookie(); update();">
<div id="whitebg"></div>
<div id="loginbox">
<h1>Pick a username:</h1>
<p><input type="text" name="pickusername" id="cusername" placeholder="Pick a username" class="msginput"></p>
<p class="buttonp"><button onclick="chooseusername()">Choose Username</button></p>
</div>
<div class="msg-container">
<div class="header">Messenger</div>
<div class="msg-area" id="msg-area"></div>
<div class="bottom"><input type="text" name="msginput" class="msginput" id="msginput" onkeydown="if (event.keyCode == 13) sendmsg()" value="" placeholder="Enter your message here ... (Press enter to send message)"></div>
</div>
<script type="text/javascript">
var msginput = document.getElementById("msginput");
var msgarea = document.getElementById("msg-area");
function chooseusername() {
var user = document.getElementById("cusername").value;
document.cookie="messengerUname=" + user
checkcookie()
}
function showlogin() {
document.getElementById("whitebg").style.display = "inline-block";
document.getElementById("loginbox").style.display = "inline-block";
}
function hideLogin() {
document.getElementById("whitebg").style.display = "none";
document.getElementById("loginbox").style.display = "none";
}
function checkcookie() {
if (document.cookie.indexOf("messengerUname") == -1) {
showlogin();
} else {
hideLogin();
}
}
function getcookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function escapehtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function update() {
var xmlhttp=new XMLHttpRequest();
var username = getcookie("messengerUname");
var output = "";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText.split("\n")
var rl = response.length
var item = "";
for (var i = 0; i < rl; i++) {
item = response[i].split("\\")
if (item[1] != undefined) {
if (item[0] == username) {
output += "<div class=\"msgc\" style=\"margin-bottom: 30px;\"> <div class=\"msg msgfrom\">" + item[1] + "</div> <div class=\"msgarr msgarrfrom\"></div> <div class=\"msgsentby msgsentbyfrom\">Sent by " + item[0] + "</div> </div>";
} else {
output += "<div class=\"msgc\"> <div class=\"msg\">" + item[1] + "</div> <div class=\"msgarr\"></div> <div class=\"msgsentby\">Sent by " + item[0] + "</div> </div>";
}
}
}
msgarea.innerHTML = output;
msgarea.scrollTop = msgarea.scrollHeight;
}
}
xmlhttp.open("GET","get-message.php?username=" + username,true);
xmlhttp.send();
}
function sendmsg() {
var message = msginput.value;
if (message != "") {
// alert(msgarea.innerHTML)
// alert(getcookie("messengerUname"))
var username = getcookie("messengerUname");
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
message = escapehtml(message)
msgarea.innerHTML += "<div class=\"msgc\" style=\"margin-bottom: 30px;\"> <div class=\"msg msgfrom\">" + message + "</div> <div class=\"msgarr msgarrfrom\"></div> <div class=\"msgsentby msgsentbyfrom\">Sent by " + username + "</div> </div>";
msginput.value = "";
}
}
xmlhttp.open("GET","update-message.php?username=" + username + "&message=" + message,true);
xmlhttp.send();
}
}
setInterval(function(){ update() }, 2500);
</script>
</body>
</html>
update-message.php
<?php
$db = new mysqli("localhost","root","","het_msg");
if ($db->connect_error) {
die("couldnt connect");
}
$username = stripslashes(htmlspecialchars($_GET['username']));
$message = stripslashes(htmlspecialchars($_GET['message']));
if ($username == "" || $message == "") {
die();
}
$result = $db->prepare("INSERT INTO messages VALUES('',?,?)");
$result->bind_param("ss", $username, $message);
$result->execute();
get-message.php
<?php
$db = new mysqli("localhost","root","","het_msg");
if ($db->connect_error) {
die("couldnt connect");
}
$username = stripslashes(htmlspecialchars($_GET['username']));
$result = $db->prepare("SELECT * FROM messages");
$result->execute();
$result = $result->get_result();
while($r = $resut->fetch_row()) {
echo $r[1];
echo "\\";
echo $r[2];
echo "\n";
}

Only 1 result from JSON array

In my getValueByKey function, I want to output 5 different items and their attributes, but my for loop only seems to allow me to choose one at a time.
When I change to i = 4 it will go to the 5th item. I just want to output all instead of choosing just one.
Any help is appreciated. Thank you very much.
window.onload = function() {
var getJSON = function (url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined'
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.setRequestHeader("X-Algolia-Application-Id", apiApplicationId);
xhr.setRequestHeader("X-Algolia-API-Key", apiKey);
xhr.responseType = 'json';
xhr.onreadystatechange = function () {
var status;
var data;
if (xhr.readyState == 4) {
status = xhr.status;
if (status == 200) {
successHandler && successHandler(xhr.response);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
var search = document.getElementById('search');
search.addEventListener('keyup', function() {
getJSON(apiEndpoint, function(data) {
function getValueByKey(key, data) {
var i, len = data.length;
for (i = 0; i < len; i++) {
if (data[i] && data[i].hasOwnProperty(key)) {
return data[i][key];
}
}
return -1;
}
document.getElementById('item-wrapper').innerHTML =
'<div class="col-sm-4">' +
'<img src="path/to-image.jpg">' +
'</div>' +
'<div class="col-sm-8">' +
'<ul>' +
'<li><strong>Bag Brand:</strong> <span id="search-results-brand"></span></li>' +
'<li><strong>Bag ID:</strong> <span id="search-results-id"></span></li>' +
'<li><strong>Bag Color:</strong> <span id="search-results-color"></span></li>' +
'<li><strong>Bag Description:</strong> <span id="search-results-description"></span></li>' +
'<li><strong>Bag Material:</strong> <span id="search-results-material"></span></li>' +
'<li><strong>Bag Price:</strong> $<span id="search-results-price"></span></li>' +
'</ul>' +
'</div>';
output_id = getValueByKey('id', data.hits);
output_brand = getValueByKey('brand', data.hits);
output_color = getValueByKey('color', data.hits);
output_description = getValueByKey('description', data.hits);
output_material = getValueByKey('material', data.hits);
output_price = getValueByKey('price', data.hits);
document.getElementById("search-results-brand").innerHTML = output_brand;
document.getElementById("search-results-id").innerHTML = output_id;
document.getElementById("search-results-color").innerHTML = output_color;
document.getElementById("search-results-description").innerHTML = output_description;
document.getElementById("search-results-material").innerHTML = output_material;
document.getElementById("search-results-price").innerHTML = output_price;
}, function(status) {
alert('Something went wrong.');
});
});
};
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/common.css">
<script type="application/ecmascript" src="js/common.js"></script>
</head>
<body>
<header>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>Test</h1>
</div>
</div>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-sm-12">
<input placeholder="search bags" id="search">
</div>
</div>
</div>
<div class="container">
<div class="row item-row" id="item-wrapper"></div>
</div>
</body>
</html>
You are return data[i][key]; . Do not return untill loop finishes.
function getValueByKey(key, data) {
var i, len = data.length;
var result =[];
for (i = 0; i < len; i++) {
if (data[i] && data[i].hasOwnProperty(key)) {
result.push(data[i][key]);
}
}
return result;
}
Edit for your question in the comment
As per my understanding your question, you can try something like this.I did not test this code. Let me know if there are any errors.
var items;
for(var i=0; i<output_id.length; i++){
items +='<div class="col-sm-4">' +
'<img src="path/to-image.jpg">' +
'</div>' +
'<div class="col-sm-8">' +
'<ul>' +
'<li><strong>Bag Brand:</strong>'+output_brand[i]+'<span id="search-results-brand"></span></li>' +
'<li><strong>Bag ID:</strong>'+output_id[i]+' <span id="search-results-id"></span></li>' +
'<li><strong>Bag Color:</strong>'+output_color[i]+' <span id="search-results-color"></span></li>' +
'<li><strong>Bag Description:</strong>'+output_description[i]+' <span id="search-results-description"></span></li>' +
'<li><strong>Bag Material:</strong>'+output_material[i]+' <span id="search-results-material"></span></li>' +
'<li><strong>Bag Price:</strong> '+output_price[i]+'$<span id="search-results-price"></span></li>' +
'</ul>' +
'</div>';
}
document.getElementById('item-wrapper').innerHTML =items;

Weird JS Behavior With Bootstrap Sliders

So I recieved help from an internet saint to vastly improve my code to create a bootstrap slider per list item within a JS for loop, but now it is behaving erratically.
Sometimes it works perfectly, others it creates new items but not sliders (just a text input field), and others it only creates one item per list.
Any great minds see where I'm going wrong?
var proArray = [];
function addPro() {
var val = document.getElementById("proInput").value.trim();
document.getElementById("proForm").reset();
if (val.length == 0) {
return;
}
if (document.getElementById('proInput' + val) == null) {
proArray.push({id: val, slider: null});
} else {
return;
}
for (var i = 0; i < proArray.length; i++) {
var ele = document.getElementById('proInput' + proArray[i].id);
if (ele == null) {
var newItem = "<li><p>" + proArray[i].id + "</p><input class='bootstrap-slider' type='text' value='' id='proInput" +
proArray[i].id + "' data-slider-id='SIDproInput" + proArray[i].id
+ "' data-slider-min='0' data-slider-max='10' data-slider-value='5'/></li>";
document.getElementById("proList").innerHTML += newItem;
proArray[i].slider = new Slider('#proInput' + proArray[i].id, {
formatter: function(value) {
return 'Current value: ' + value;
}
});
} else {
(function(i) {
setTimeout(function() {
var val = proArray[i].slider.getValue();
proArray[i].slider.destroy();
document.getElementById('SIDproInput' + proArray[i].id).remove();
proArray[i].slider = new Slider('#proInput' + proArray[i].id, {
formatter: function (value) {
return 'Current value: ' + value;
}
});
proArray[i].slider.setValue(val);
}, 100);
})(i);
}
}
}
var conArray = [];
function addCon() {
var valCon = document.getElementById("conInput").value.trim();
document.getElementById("conForm").reset();
if (valCon.length == 0) {
return;
}
if (document.getElementById('conInput' + valCon) == null) {
conArray.push({id: valCon, slider: null});
} else {
return;
}
for (var i = 0; i < conArray.length; i++) {
var ele = document.getElementById('conInput' + conArray[i].id);
if (ele == null) {
var newItem = "<li><p>" + conArray[i].id + "</p><input class='bootstrap-slider' type='text' value='' id='conInput" +
conArray[i].id + "' data-slider-id='SIDconInput" + conArray[i].id
+ "' data-slider-min='0' data-slider-max='10' data-slider-value='5'/></li>";
document.getElementById("conList").innerHTML += newItem;
conArray[i].slider = new Slider('#conInput' + conArray[i].id, {
formatter: function(value) {
return 'Current value: ' + value;
}
});
} else {
(function(i) {
setTimeout(function() {
var valCon = conArray[i].slider.getValue();
conArray[i].slider.destroy();
document.getElementById('SIDconInput' + conArray[i].id).remove();
conArray[i].slider = new Slider('#conInput' + conArray[i].id, {
formatter: function (value) {
return 'Current value: ' + value;
}
});
conArray[i].slider.setValue(valCon);
}, 100);
})(i);
}
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.3/css/bootstrap-slider.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.3/bootstrap-slider.min.js"></script>
<div class="col-sm-6">
<h2>Pros</h2>
<p>The Good Stuff</p>
<form id="proForm" onkeypress="return event.keyCode != 13;">
<input class="form-control text-left pro-con-input" id="proInput" placeholder="Add New Benefit"/>
<div onclick="addPro()" class="btn pro-con-btn">Add</div>
</form>
<h3 class="text-left">Benefits</h3>
<ul class="text-left" id="proList">
</ul>
</div> <!-- pros -->
<div class="col-sm-6">
<h2>Cons</h2>
<p>The Bad Stuff</p>
<form id="conForm" onkeypress="return event.keyCode != 13;">
<input class="form-control text-left pro-con-input" id="conInput" placeholder="Add New Benefit"/>
<div onclick="addCon()" class="btn pro-con-btn">Add</div>
</form>
<h3 class="text-left">Costs</h3>
<ul class="text-left" id="conList">
</ul>
</div> <!-- cons -->
Because you have two lists you can use two arrays:
var proArray = [];
var conArray = [];
The inline functions can be changed in order to pass the list prefix as parameter:
newAdd('pro')
newAdd('con')
And so you can adjust the addPro function to these changes.
From comment:
If I type in "#" or "?" as an item in your snippet above it shows the error. Not for you?
In order to solve such an issue you need to escape those chars when creating the slider:
arr[i].slider = new Slider('#' + listIdPrefix + 'Input' +
arr[i].id.replace(/#/g, '\\#').replace(/\?/g, '\\?').....
The snippet:
var proArray = [];
var conArray = [];
function newAdd(listIdPrefix) {
var val = document.getElementById(listIdPrefix + "Input").value.trim();
document.getElementById(listIdPrefix + "Form").reset();
if (val.length == 0) {
return;
}
var arr;
if (document.getElementById(listIdPrefix + 'Input' + val) == null) {
if (listIdPrefix == 'pro') {
proArray.push({id: val, slider: null});
arr = proArray;
} else {
conArray.push({id: val, slider: null});
arr = conArray;
}
} else {
return;
}
for (var i = 0; i < arr.length; i++) {
var ele = document.getElementById(listIdPrefix + 'Input' + arr[i].id);
if (ele == null) {
var newItem = "<li><p>" + arr[i].id + "</p><input class='bootstrap-slider' type='text' value='' id='" + listIdPrefix + "Input" +
arr[i].id + "' data-slider-id='SID" + listIdPrefix + "Input" + arr[i].id
+ "' data-slider-min='0' data-slider-max='10' data-slider-value='5'/></li>";
document.getElementById(listIdPrefix + "List").innerHTML += newItem;
arr[i].slider = new Slider('#' + listIdPrefix + 'Input' + arr[i].id.replace(/#/g, '\\#').replace(/\?/g, '\\?').replace(/\./g, '\\.'), {
formatter: function (value) {
return 'Current value: ' + value;
}
});
} else {
(function (i, arr) {
setTimeout(function () {
var val = arr[i].slider.getValue();
arr[i].slider.destroy();
document.getElementById('SID' + listIdPrefix + 'Input' + arr[i].id).remove();
arr[i].slider = new Slider('#' + listIdPrefix + 'Input' + arr[i].id.replace(/#/g, '\\#').replace(/\?/g, '\\?').replace(/\./g, '\\.'), {
formatter: function (value) {
return 'Current value: ' + value;
}
});
arr[i].slider.setValue(val);
}, 100);
})(i, arr);
}
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.3/css/bootstrap-slider.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.3/bootstrap-slider.min.js"></script>
<div class="col-sm-6">
<h2>Pros</h2>
<p>The Good Stuff</p>
<form id="proForm" onkeypress="return event.keyCode != 13;">
<input class="form-control text-left pro-con-input" id="proInput" placeholder="Add New Benefit"/>
<div onclick="newAdd('pro')" class="btn pro-con-btn">Add</div>
</form>
<h3 class="text-left">Benefits</h3>
<ul class="text-left" id="proList">
</ul>
</div> <!-- pros -->
<div class="col-sm-6">
<h2>Cons</h2>
<p>The Bad Stuff</p>
<form id="conForm" onkeypress="return event.keyCode != 13;">
<input class="form-control text-left pro-con-input" id="conInput" placeholder="Add New Benefit"/>
<div onclick="newAdd('con')" class="btn pro-con-btn">Add</div>
</form>
<h3 class="text-left">Costs</h3>
<ul class="text-left" id="conList">
</ul>
</div>

Getting JSON Object then adding parts to HTML

I have been trying to get a script to work for my Github site and I am processing a JSON file to get an array to HTML in a ordered list.
My code I have been using is:
$(document).ready(function() {
$.getJSON("https://raw.githubusercontent.com/vvoid-inc/LillyPak-Starbound/gh-pages/mods/stable.json", function(data) {
$.each(mods, function(i) {
var tempUrl = "";
var tempApproved = "";
if (data.mods[i].Repo[0] == 0) {
tempUrl = data.mods[i].Repo[1];
} else if (data.mods[i].Repo[0] == 1) {
tempUrl = "http://community.playstarbound.com/resources/" + data.mods[i].Repo[1] + "/";
} else {
tempUrl = data.mods[i].Repo[1];
}
if (data.mods[i].Approved == true) {
tempApproved = "checked";
} else {
tempApproved = "";
}
$("#modsList").append("<li><a href='" + tempUrl + "'>" + data.mods[i].Title + "</a> - by " + data.mods[i].Author + "<ul><li>\t- <input type='checkbox' " + tempApproved + "/> Approved</li></ul></li>");
});
console.log(data);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ol id="modsList"></ol>
So what have I been doing wrong because nothing is working...
This should work:
$(document).ready(function() {
$.getJSON("https://raw.githubusercontent.com/vvoid-inc/LillyPak-Starbound/gh-pages/mods/stable.json", function(data) {
$.each(data.mods, function(i, m) {
var tempUrl = "";
var tempApproved = "";
if (m.Repo[0] == 0) {
tempUrl = m.Repo[1];
} else if (m.Repo[0] == 1) {
tempUrl = "http://community.playstarbound.com/resources/" + m.Repo[1] + "/";
} else {
tempUrl = m.Repo[1];
}
if (m.Approved == true) {
tempApproved = "checked";
} else {
tempApproved = "";
}
$("#modsList").append("<li><a href='" + tempUrl + "'>" + m.Title + "</a> - by " + m.Author + "<ul style='list-style-type:none'><li>\t - <input type='checkbox' " + tempApproved + " disabled/> Approved</li></ul></li>");
});
console.log(data);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ol id="modsList"></ol>
Instead of $.each(mods, function(i) { it should be $.each(data.mods, function(i, m) {. and the mods[i]. should be m..
Also there was a formatting error in the JSON file but now it is fixed. It was causing the errors to not display.

Unexpected token } can not find the mistake

i tried everything but i don't know where the mistake should be its show me a unespekted token } in a line where i not even have this sign.
<script type="text/javascript">
function postToStatus(action,type,user,ta){
var data = _(ta).value;
if(data == ""){
alert("Type something first weenis");
return false;
}
_("statusBtn").disabled = true;
var ajax = ajaxObj("POST", "php_parsers/qp_system.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
var datArray = ajax.responseText.split("|");
if(datArray[0] == "post_ok"){
var sid = datArray[1];
data = data.replace(/</g,"<").replace(/>/g,">").replace(/\n/g," ><br />").replace(/\r/g,"<br />");
var currentHTML = _("statusarea").innerHTML;
_("statusarea").innerHTML = '<div id="status_'+sid+
'class="status_boxes"><div><b>Posted by you just now:</b><span >id="sdb_'+sid+
'"><a href="#" onclick="return false" >onmousedown="deleteStatus("'+sid+
'\',\'status_'+sid+
'\');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a> ></span><br />'+data+
'</div></div>"'+currentHTML;
_("statusBtn").disabled = false;
_(ta).value = "";
} else {
alert(ajax.responseText);
}
}
}
ajax.send("action="+action+"&type="+type+"&user="+user+"&data="+data);
}
You need to put a semi-colon after the last } just before the ajax.send() call.
This website is your best friend if you write JavaScript: http://closure-compiler.appspot.com/home
function postToStatus(action, type, user, ta) {
var data = _(ta).value;
if (data == "") {
alert("Type something first weenis");
return false;
}
_("statusBtn").disabled = true;
var ajax = ajaxObj("POST", "php_parsers/qp_system.php");
ajax.onreadystatechange = function() {
if (ajaxReturn(ajax) == true) {
var datArray = ajax.responseText.split("|");
if (datArray[0] == "post_ok") {
var sid = datArray[1];
data = data.replace(/</g, "<").replace(/>/g, ">").replace(/\n/g, " ><br />").replace(/\r/g, "<br />");
var currentHTML = _("statusarea").innerHTML;
_("statusarea").innerHTML = '<div id="status_' + sid + 'class="status_boxes"><div><b>Posted by you just now:</b><span >id="sdb_' + sid + '">delete status ></span><br />' + data + '</div></div>"' + currentHTML;
_("statusBtn").disabled = false;
_(ta).value = "";
} else {
alert(ajax.responseText);
}
}
};
ajax.send("action=" + action + "&type=" + type + "&user=" + user + "&data=" + data);
}
;

Categories