convert js output to objective C - javascript

I am doing work on iphone app in which I have also used js as well as css files along with objective C to get better output. But at one point I stuck out. I am using the following js code which is showing the alert. I have attached the image also which is representing the alert by clicking on OK button.
**NSString *someHtmlContent1 = #"<script src='jquery.min.js'>
</script>
<script src='jquery-ui.min.js'></script>
<script src='jquery.ui.touch-punch.min.js'></script>
<script>
function result(qn) {
var u = document.getElementById('sortable'+qn);
var LIs = u.getElementsByTagName('li');
var list = [];
for( var i = 0; i < LIs.length; ++i ){
var LI = LIs[i];list.push(LI.innerHTML);
}
res = list;
var i=0;
var str = '';
for (i in res) {
str = str+' '+res[i];
}
alert(str);
return str;
}
</script>";**
I want to get the content**(.......)** in NSString which is showing on alert. Anyone know that how to convert js variables to objective C? Please do help me.
Thanks to all.

The only trick to make a link between JS and objective-C is the following one :
In your JS, call a link like window.location = "something://smth/smthelse"
And in the Objective-C, in your web view delegate method (webView:shouldStartLoadWithRequest:navigationType: ), you can handle this call and do something with it.

Related

Error saving this JS in WSO2-Integration Studio

I use Integration Studio,
and I have always used inside my Xml files js script,
But this time I still get an error saving this inline JS, and I really can't understand the reason of this error.
<script language="js"><![CDATA[var elements = mc.getProperty('elementsFromJson');
var payload = [];
for(let i = 0; i <= elements.length; i++){
var id= elements[i].id;
var type= elements[i].type;
var couple= {"id":id ,"type":type};
print(couple);
payload.push(couple);
}
var payloadFinale = {payload};
print('result in JSON: ' + payloadFinale);
var data= mc.setPayloadJSON(payloadFinale);]]></script>
So would be great your help in case you use Integration studio as me,
thank you for your time.
You have issues in the following lines.
for(let i = 0; i <= elements.length; i++){
Here change let => var
var payloadFinale = {payload};
The above syntax is wrong, not sure what you are trying to achieve here. Probably, you can remove the above line, when you do mc.setPayloadJSON it would be saved as a proper JSON.

javascript: how to pass an array argument from an in-html script to an external JS function

I'm pretty new at Javascript (and web design). I'm trying to design an interactive webgame for purely educational purposes (I'm a professional educator. Most of my programming experience in creating self-contained Java apps).
In my HTML document, I have some internal script that creates a boolean array called hypotheses. I also have an external JS script called game.js that produces some visual output based on its own array, also called hypotheses, which I call using . I would like to update the external array from the internal one, but nothing I try works. The internal script is called from a form.
<script>
var testedhypotheses = [];
var hypotheses = hypArray(); //load up a boolean array of hypotheses.
function testHypothesis(theform, numTest){
//update numbers
theform.newhyp.value = theform.newhyp.value - numTest;
//move hypotheses from one list to the other
for(var i; i < numTest; i++){
testedHypotheses.push(hypotheses.pop());
}
//pass the array of hypotheses to the external function.
updateHyp(testedHypotheses);
</script>
The function definitely works, and items are being moved from one array to another. However, the function updateHyp() is not being called. I've tried it a bunch of ways, but the most recent was to include this in the HTML file:
<script type="text/javascript" src="game.js">
var updateHyp = function (hypArray) {
updateHypotheses(hypArray);
};
</script>
Inside game.js, I have the following code:
var updateHypotheses = function(hypArray){
var end = hypotheses.length;
for(var i = end; i < hypArray.length; i++){
var ind = hypotheses.length;
var tf = hypArray[i];
var rando = Math.random();
var res rando < 0.5 ? 1 : 0;
hypotheses.push(new Hypothesis(i, tf, res));
}
}
};
It seems that this code is never getting called. Any help would be much appreciated!!!

collect data by id using javascript

I have to save temporary data for my webpage using java script.This is the way that i save i one by one since the data is an array.
var product= new Array();
product[1] = document.getElementById("product[1]").value;
product[2] = document.getElementById("product[2]").value;
This method is working. but when i run it by looping, it doesnt work.
for(var i=1; i < no_item; i++){
product[i] = document.getElementById("product[i]").value;
}
*product[] is a varibale that I take from a html dropdown menu
Can anyone please tell me the problem ? thanks ~ =)
Should be written as, as you are going to be getting the id "product[i]" every time with your original code. This will get "product[1]" then "product[2]" and so on:
for(var i=1; i < no_item; i++){
product.push(document.getElementById("product[" + i + "]").value);
}
Also, as a comment, we tend to prefer var product = []; over var product = new Array(); in javascript but both will work.

How to get the next element of an array with Jquery onclick

Hi all i am trying to change the html of an object from an array of htmls. But i am having problem iterating properly. I managed to make it work once
EDIT
After a few complains about the clarity of my question I will rephrase it. I have a div panel called .trpanel and a button called #trigger2 (it is a next button). Then I have a series of divs with texts that contain translations. I want when I press the button (called next) to cycle through the translations one by one on the trpanel.
var ltranslation = [];
ltranslation[0] = $("#translation-en-1").html();
ltranslation[1] = $("#translation-ur-en").html();
ltranslation[2] = $("#translation-fr-en").html();
ltranslation[3] = $("#translation-it-en").html();
ltranslation[4] = $("#translation-sp-en").html();
ltranslation[5] = $("#translation-po-en").html();
ltranslation[6] = $("#translation-fr-en").html();
ltranslation[7] = $("#translation-de-en").html();
var l= ltranslation;
$("#trigger2").off('click').on('click',function(){
for (var i = 0; i <= ltranslation.length; i++){
if (i==7){i=0;}
$(".trpanel").html.ltranslation[i]; or ???//replace().ltranslation[]+i??? the code throws errors
}
});
I am quite new to Javascript and i am getting a bit confused with the types of objects and arrays and loops. I managed once to add the htmls but without replacing them ... so they all came one after the other. The i tried to change the code and it hasn't worked since. Any help will be greatly appreciated.
A lot of guessing, but seems like you are trying to do this :
var trans = $('[id^="translation-"]'),
idx = 0;
$("#trigger2").on('click',function(){
$(".trpanel").html( trans.eq(idx).html() );
idx = idx > 6 ? 0 : idx+1;
});
FIDDLE
I think you are trying to do this:
if (i == 7) {
i = 0; // I don't really know why you are doing this, but it will reset the loop
}
$(".trpanel").html(ltranslation[i]); //I'm passing ltranslation[i] to the html method. Instead of .html.ltranslation[i].
}
Also, without seeing any html, I'm not sure but I think you may want to iterate over .trpanel ?
Something like:
$(".trpanel").eq(i).html(ltranslation[i]);
Another thing (so you can make your code clearer I think). You can abstract the array population in a function, like this:
var ltranslation = [];
var languages = ["en-1", "ur-en", "fr-en", "it-en", "sp-en", "po-en", "fr-en", "de-en"];
$.each(languages, function(index) {
ltranslation[index] = $("#translation-" + this).html();
});
// Then you can use ltranslation
If you want to flip through several translations I would implement it that way:
var translations=["hej","hello", "hallo","hoy"];
var showTranslation=function(){
var current=0;
var len=translations.length;
return function(){
var direction=1;
if (current>=len) current=0;
$("#text").text(translations[current]);
current+=direction;
}
}();
$("#butt").on("click", showTranslation);
Fiddle: http://jsfiddle.net/Xr9fz/
Further: You should give your translations a class, so you could easily grab all of them with a single line:
$(".translation).each(function(index,value){ ltranslation.push(value); })
From the question : I managed once to add the htmls but without replacing them -
I think you want to add all of these items into $(".trpanel"). First, dont take the HTML of each element, clone the element itself :
//method ripped from Nico's answer.
var ltranslation = [];
var languages = ["en-1", "ur-en", "fr-en", "it-en", "sp-en", "po-en", "fr-en", "de-en"];
$.each(languages, function(index) {
ltranslation[index] = $("#translation-" + this).clone();
});
Then you could append everything into the container, so add the htmls but without replacing them. append takes in an array without replacing the previous html.
$("#trigger2").off('click').on('click',function() {
$(".trpanel").append(ltranslation);
});
I don't know what exactly you're tring to do, but I've put comments in your code to help you better understand what your code is doing. The net effect of your code is this (which I doubt you want) :
$("#trigger2").off('click').on('click',function(){
$(".trpanel").html(ltranslation[7]);
});
This is your code with some comments and minor changes
var ltranslation = [];
ltranslation[0] = $("#translation-en-1").html();
ltranslation[1] = $("#translation-ur-en").html();
ltranslation[2] = $("#translation-fr-en").html();
ltranslation[3] = $("#translation-it-en").html();
ltranslation[4] = $("#translation-sp-en").html();
ltranslation[5] = $("#translation-po-en").html();
ltranslation[6] = $("#translation-fr-en").html();
ltranslation[7] = $("#translation-de-en").html();
var l= ltranslation;
$("#trigger2").off('click').on('click',function(){
for (var i = 0; i < ltranslation.length; i++){
//if (i==7){i=0;} <-- This will cause an infinite loop won't it? are you trying to reset i? i will reset next time loop is called,
$(".trpanel").html(ltranslation[i]); //<-- this will overwrite elements with class .trpanel ltranslation.length times...
///you'll see only the value of translation[7] in the end
}
});
EDIT
To do what you want to do based on your comments, try this:
var ltranslation = [];
ltranslation[0] = $("#translation-en-1").html();
ltranslation[1] = $("#translation-ur-en").html();
ltranslation[2] = $("#translation-fr-en").html();
ltranslation[3] = $("#translation-it-en").html();
ltranslation[4] = $("#translation-sp-en").html();
ltranslation[5] = $("#translation-po-en").html();
ltranslation[6] = $("#translation-fr-en").html();
ltranslation[7] = $("#translation-de-en").html();
var counter = 0;//a global counter variable
$("#trigger2").click(function(){ //eeverytime button is clicked do this
$(".trpanel").html(ltranslation[counter]); //set the html to an element of array
counter++; //increment counter
if(counter==ltranslation.length) //reset the counter if its bigger than array len
counter=0;
});

How to get hash tag in javascript SRC

Is there easy way to get the hash tag in script source?
Example:
<script src='myscript.js#result=5235'></script>
I want to get the variable result=5235 using myscript.js
http://jsfiddle.net/cjc343/h6Zzw/
You'd probably want to use a more specific selector than that, but it gets the point across.
Edit: Why did you tag jQuery if you don't want jQuery?
Pure: http://jsfiddle.net/cjc343/h6Zzw/1/
var hash = $('script').attr('src').split('#')[1];
or
var hash = document.getElementsByTagName('script')[0].src;
hash = hash.split('#')[1];
assuming it's the first script, otherwise change [0] to whatever index the script has.
var srcVariable = $(script).attr("src");
var hashText = srcVariable.substring(srcVariable.indexOf('#'));
For this example suppose you want to do your operation of getting that result=whatever to all of your script tags on your page. Do something like below:
var whatever = [];
$('script').each(function(){
var src = $(this).attr('src');
var splitonhash-arrReturned = src.split('#');
var yourAnswer = splitonhash-arrReturned[1];
whatever.push(yourAnswer);
});
PURE JS example:
var whatever = [];
var items = document.getElementsByTagName('script');
for (i=0; i< items.length; i++)
{
whatever.push(items[i].attributes.getNamedItem("src").split('#')[1]);
}

Categories