I have to change my project form XML to JavaScript (at least some parts of it).
So I had construction like this :
$xml = simplexml_load_file("http://...);
$profile = $xml->profile;
$user_id = $profile->user_id;
Now I wanted to translate this into JavaScript so I used :
var xmlHttp_subscribe = new XMLHttpRequest();
xmlHttp_subscribe.onreadystatechange=postCall;
xmlHttp_subscribe.open("GET","http://...",true);
xmlHttp_subscribe.send();
and now function postCall()
function postCall(){
var t = document.getElementsByName("MY_API").value;
alert('t'+t);
var p = document.getElementsByName("profile").value;
alert('p'+p);
var h = document.getElementsByName("user_id").value;
//...//
}
The XML is under my http:// is like that :
<MY_API>
<profile>
<user_id>the_user_id</user_id>
</profile>
</MY_API>
What I would like to do is to get this 'the_user_id' part as string in plain text.
Does any one have any idea how to do this?
Am I looking in the good direction?
Thanks for any kind of help.
There is no function "getElementsByName". What you need is getElementsByTagName.
Check this link out, it should be what you're looking for
As suggested by Pineda, the right function name is getElementsByTagName, and in addition the right property name is not "value" but nodeValue, so you should use
function postCall(){
var h = document.getElementsByTagName("user_id").nodeValue;
}
Related
I'm not very good at filtering and wanted to write a custom filter based on the following:
I call a service that returns a JSON object with HTML String thats concatenated with another string - so the HTML is funky.
I want to get the text1 and text2 form the following returned HTML string:
<span><b>text1</b><b>text2</b>text3</span>
I have no control how the above is returned to me, but i just wanted to get the two values and concatenate them: text1text2
There is a builtin DOM parser - or you can find a parser in your environment. See on MDN parsing XML and Element. So you could do something like this:
var x = "<span><b>text1</b><b>text2</b>text3</span>";
var oDOM = new DOMParser().parseFromString(x, "text/xml");
var b = oDOM.documentElement.getElementsByTagName("b");
b.length // 2
b[1].innerHTML // text2
HTH
if you just need to strip the html tags, I think you can use the below code
var noHTML = OriginalString.replace(/(<([^>]+)>)/ig,"");
For a filter implementation
angular.module('myNoHtmlFilterApp', [])
.filter('noHtml', function() {
return function(input) {
input = input || '';
var out = input.replace(/(<([^>]+)>)/ig,"");
return out;
};
})
Based on DineSH's answer - I did something like this:
$scope.getTextFromHTML = function(html){
var oDOM = new DOMParser().parseFromString(html, "text/xml");
var b = oDOM.documentElement.getElementsByTagName("b");
return b[0].innerHTML+b[1].innerHTML;
};
I have to pre-define the html string since its not on the DOM yet, like so:
var html = "<span><b></b><b></b></span>";
I will probably add a for loop later in case there is a string I missed, but for now, this is perfect. Thank you for all of your help!
currently trying to parse the download link for zippyshare files in php the issue is I need to get their javascript and I am not being able to do it. This is the part of the page I need to parse:
<script type="text/javascript">
var somdfunction = function() {
var a = 327030;
document.getElementById('dlbutton').omg = 327033%78956;
var b = parseInt(document.getElementById('dlbutton').omg) * (327033%3);
var e = function() {if (false) {return a+b+c} else {return (a+3)%b + 3}};
document.getElementById('dlbutton').href = "/d/91667079/"+(b+18)+"/Animals%20%28Radio%20Edit%29-www.manomuzika.net.mp3";
if (document.getElementById('fimage')) {
document.getElementById('fimage').href = "/i/91667079/"+(b+18)+"/Animals%20%28Radio%20Edit%29-www.manomuzika.net.mp3";
}
var result = 0;
}
</script>
Which being fetched from its website using:
$html = file_get_html($url);
Basically they create the download links dynamically using javascript, I am able to get the source using my parser but I need to cut it down to getting the values of:
var a = 327030;
document.getElementById('dlbutton').omg = 327033%78956;
and finally
document.getElementById('dlbutton').href = "/d/91667079/"+(b+18)+"/Animals%20%28Radio%20Edit%29-www.manomuzika.net.mp3";
Once I am able to get these three variables from within the source I will be able to create the download link my issue at the moment is cutting it down to that.
I am using this parser:
http://simplehtmldom.sourceforge.net/
If you would like to see the source code I am able to parse at the moment here it is:
http://www.somf.us/music/test.php?url=http://www66.zippyshare.com/v/91667079/file.html
You need to use regex because simple is not a javascript parser.
Here's a hint to get you started:
preg_match('/var a = (\d+);/', file_get_contents($url), $m);
echo $m[1];
I'm trying to get the link element in a feed, i can get the description and title, but cannot get the link element. It seems weird to me. Here is my code
var url = "http://healthyhow.net/feed";
var response = UrlFetchApp.fetch(url);
var shareDoc = Xml.parse(response.getContentText(), true);
var root = shareDoc.getElement(); // feed element
var entries = root.getElement("channel").getElements("item");
for (var i=0; i<1; i++) { //just pick the first entry
var e = entries[i];
var title = e.getElement("title").getText();
var link = e.getElement("link").getText();
var description = e.getElement("description").getText();
}
Could anyone point out what is wrong here?
Thanks!
The docs indicate you should use lenient parsing for HTML - it's unclear what this is doing underneath, but in your case maybe it's confusing an HTML <link> element with a generic XML element with that same tag name. It appears to parse the link entries into something like this for your code (which you can see in shareDoc.toXmlString()):
<link/>http://healthyhow.net/l-arginine-natural-treatment-for-hypertension/
Since it's an empty tag, no text.
Change:
var shareDoc = Xml.parse(response.getContentText(), true);
to be:
var shareDoc = Xml.parse(response.getContentText(), false);
and you should be able to get the link text.
I noticed that some validation tools use the "class" attribute to pass options to validation scripts.
Example:
<input class="input-field validate[required, email]" name="" type="text" />
the "required" and "email" would be picked up by the script as setting options.
I was wondering is there an easy way to achieve this or maybe this is already available in jQuery?
I want to use this as a means of passing certain settings to my script.
Any help would be appreciated,
Thanks
Edit:
Thanks #loseb your example works great.
Another thing I am trying to achieve by doing some small modification to your example:
function classAttributes( classString, className ) {
var data;
var regex = new RegExp(className+"\[(.*?)\]");
var matches = classString.match(regex);
if ( matches ) {
//matches[1] refers to options inside [] "required, email, ..."
var spec = matches[1].split(/,\s*/);
if ( spec.length > 0 ) {
data = spec;
}
}
return data;
}
any idea why "new RegExp(className+"[(.*?)]");" doesnt work. var matches is blank.
Edit:
second part of the question moved to:
javascript regex pattern with a variable string not working
If I understand you correctly this is the solution:
var className = $('.input-field').att('class');
var regex = /validate\[(.*?)\]/;
var matches = className.match(regex);
if (matches) {
//matches[1] refers to "required, email" string
var spec = matches[1].split(/,\s*/);
if (spec.length == 2) {
var attribute = spec[0]; //required or optional or anything else
var validator = spec[1]; //actual validation type
}
}
I would just use data to do this instead:
var mydata={required: true,email:false};
$('.input-field').data('validate',mydata);
see an example here:http://jsfiddle.net/MarkSchultheiss/FzaA8/
How would you go about breaking up a textarea value into an array, based on the end of line separation? Use of jQuery is cool by me...
This should work (tested in Firefox and Google Chrome):
var arrayOfLines = $('#textAreaID').val().split('\n');
Cross-platform way:
var area = document.getElementById("area");
var lines = area.value.replace(/\r\n/g,"\n").split("\n");
var stringArray = document.getElementById('textarea').value.split('\n');
I like the "cross-platform way" answer best (https://stackoverflow.com/a/32240738/34806) as I've grappled with input from a Mac in the past. Nevertheless I think most of the existing answers could benefit from an additional step.
Specifically, what if some lines are empty? The following will filter out such lines so that we wind up with a "compact" array rather than a "sparse" one (or at least, rather than one with elements containing no values)
var area = document.getElementById("area");
var lines = area.value.replace(/\r\n/g,"\n").split("\n").filter(line => line);
You could try this function :
function textToArray(){
var someArray = [];
var nameList = $("#txtArea").val();
$.each(nameList.split(/\n/), function (i, name) {
// empty string check
if(name != ""){
someArray.push(name);
}
});
taken from : CONVERT TEXTAREA CONTENT TO AN ARRAY USING JQUERY
This method worked well:
var textArea = document.getElementById("textAreaId");
var arrayFromTextArea = textArea.value.split(String.fromCharCode(10));