Hi i at the momment try to parse some HTML news for our new fan page.
Caus the company do not offer a RSS Feed.
I got a new JS File with that included
function getNews() {
y = 0;
news = new Array(7);
news_content = new Array(5);
for (var i = 0; i < news.length; i++)
{
var table = document.getElementById('news').contentWindow.getElementsByTagName('table')[y];
news_content[0] = table.rows[0].cells[0].getElementsByTagName('img')[0].src;
news_content[1] = table.rows[0].cells[1].getElementsByTagName('span')[0].innerHTML;
news_content[2] = table.rows[0].cells[2].getElementsByTagName('span')[0].innerHTML;
news_content[3] = table.rows[1].cells[0].getElementsByTagName('p')[0].innerHTML;
news_content[4] = table.rows[0].cells[0].getElementsByTagName('a')[0].href;
//alert(news[0] + "\n" + news[1] + "\n" + news[2] + "\n" + news[3] + "\n" + news[4]);
news[i] = news_content[0] + "\n" + news_content[1] + "\n" + news_content[2] + "\n" + news_content[3] + "\n" + news_content[4] + "\n";
y = y + 2;
}
alert (news[0] + "\n" + news[1] + "\n" + news[2] + "\n" + news[3] + "\n" + news[4])
}
and that html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
<script src="test.js"></script>
</head>
<body>
Hier klicken
<iframe id="news" src="http://www.aerosoft-shop.com/list_news.php?cat=fs&lang=de">
</body>
</html>
At last if i pase the source code into the html file it works but is there no way to parse from a external page?
If you debug your code with a tool like Firebug, a errormessage would be returned like this:
Permission denied to access property 'getElementsByTagName'
It's indeed not possible in JavaScript to access a IFrame which points to a different domain, not even subdomain of your domain (according to the comment on this answer it is possible).
The question here is, if the site-owner wants you do crawl his site off or at least gave you an okay for it, because its generally not that welcomed to get crawled from other sources (traffic and maybe copyright problems).
Related
I hhave this in the html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Geolocation</title>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script src="funciones.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>
<button id="startGeo">Click here to check your geolocation abilities</button>
</body>
</html>
and I have this in a function.js file:
$(document).ready(function(){
$("#startGeo").click(checkLocation);
function checkLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLocation,locationFailed);
//document.write("you have geolocation");
}
else {
document.write("you don't have geolocation");
}
}//ends checkLocation()
function getLocation(position){
var latitud = position.coords.latitude;
var longitud = position.coords.longitude;
var exactitud = position.coords.accuracy;
alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
//document.write("we received your location");
}
function locationFailed(){
document.write("we didn't get your location. Please check your settings");
}
});
from this I get the coordinates, but I have no idea at all on how to get the city name... or the state name. I saw a question similar to this answered but they seemed to be using json. I don't want a map, just the info, it can be text or an alert. Please any ideas?
You can use reverse Geocoding API
Sample call:
http://maps.googleapis.com/maps/api/geocode/json?latlng=50.123413,-22.12345&sensor=true
You can replace your getLocation function like that (make appropriate tests before accessing data.results[2]):
function getLocation(position){
var latitud = position.coords.latitude;
var longitud = position.coords.longitude;
var exactitud = position.coords.accuracy;
$.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud + '&sensor=true', function(data) {
alert(data.results[2].address_components[0].long_name);
});
//alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
//document.write("we received your location");
}
You can check the following fiddle to see it in action:
https://jsfiddle.net/5tzxks10/
+Nowres Rafed I changed your code a little bit:
function getLocation(position){
var latitud = position.coords.latitude;
var longitud = position.coords.longitude;
var exactitud = position.coords.accuracy;
$.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitud + ',' + longitud, function(data) {
alert(data.results[1].formatted_address);
});
//alert("latitud: " + latitud + " longitud: " + longitud + " exactitud: " + exactitud);
//document.write("we received your location");
it gives more information that way. Thank you so much. What I would like to know (if it's possible) is where can I read or know how to fetch a specific answer, I read that when you get the results, it returns more than one result. I changed the number as you mentioned, testing the different options. But, is there somewhere where I can look for those results? forgive the bother, I'm just new to this.
I found working JavaScript code to feed into a jQuery mobile app here and made some minor modifications to it.
As I am very new to JavaScript - currently in the beginning of the JavaScript course at codeacademy.com - I didn't realize that document.write is not the best way to output/build your HTML.
When the .js file is loaded it's like a blank page which I believe is due to document.write. When I refresh, the RSS feed displays correctly. I'm having a very hard time figuring out what code I need to use to replace the document.write portions.
I've seen document.getElementById('div1').innerHTML='test' but not quite sure how to use this to replace document.write in the code below.
If someone would provide the alternative code for document.write in one of the sections below before the JSON code I would be extremely grateful.
/* configuration */
var maxLength = 10;
/* writing HTML */
document.write(
/* page begins */
'<div id="news" data-role="page" data-theme="a" data-title="NEWS">' +
' <div data-role="content">' +
' <ul data-role="listview" data-filter="true" id="dynamiclist" data-inset="true">'
);
for (var i=1; i<=maxLength; i++){
document.write(
'<li id="list' + i + '"> </li>'
);
}
document.write(
' </ul>' +
' </div>' +
'</div>'
);
for (i=1; i<=maxLength; i++){
document.write(
'<div data-role="page" id="article' + i + '">' +
' <div data-role="content">' +
' <h3 id="articleHeader' + i + '"> </h3>' +
' <div id="articleContent' + i + '" class="articleContent">' +
' <p id="articleDate' + i + '" class="articleDate"></p></div>' +
' <div data-role="controlgroup" data-type="horizontal">' +
' News' +
' <a href="#article' + String(i-1) + '" data-role="button" data-icon="arrow-l"' +
' data-inline="true" class="prevButton">Prev</a>' +
' <a href="#article' + String(i+1) + '" data-role="button" data-icon="arrow-r"' +
' data-inline="true" class="nextButton" data-iconpos="right">Next</a>' +
' </div>' +
' </div>' +
'</div>'
);
}
/* JSONP */
$(function(){
getOnlineFeed('http://rss.cnn.com/rss/money_news_economy.rss');
});
/* functions */
var getOnlineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', 'http://ajax.googleapis.com/ajax/services/feed/load?callback=listEntries&hl=ja&output=json-in-script&q='
+ encodeURIComponent(url)
+ '&v=1.0&num=' + maxLength);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
var getOfflineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
var listEntries = function(json) {
if (!json.responseData.feed.entries) return false;
$('#widgetTitle').text(json.responseData.feed.title);
var articleLength =json.responseData.feed.entries.length;
articleLength = (articleLength > maxLength) ? maxLength : articleLength;
for (var i = 1; i <= articleLength ; i++) {
var entry = json.responseData.feed.entries[i-1];
$('#link' + i).text(entry.title);
$('#articleDate' + i).text(entry.pubdate);
$('#articleHeader' + i).text(entry.title);
$('#openButton' + i).attr('href', entry.link);
$('#articleContent' + i).append(entry.content);
}
$('#article1 .prevButton').remove();
$('#article' + articleLength + ' .nextButton').remove();
if (articleLength < maxLength) {
for (i = articleLength + 1; i <= maxLength; i++) {
$('#list' + i).remove();
$('#article' + i).remove();
}
}
};
$('#PageRefresh').click(function() {
location.reload();
});
Edit: I did not read the question carefully enough the first time - I now see that you're already using jQuery, but not before you do your DOM manipulation. So the rest of the answer stands - use .html() and manipulate the DOM only after the document is ready.
Here's my suggestion. Full disclaimer: this is not the only way to do it nor is it necessarily the "best" way to do it. I daresay it would be the most common way to do it, however.
Use jQuery - it will make your life so much easier. http://jquery.com
Make sure you do your DOM (Document Object Model) manipulation (inserting HTML elements to your document is a type of DOM manipulation, although I'm not sure that document.write() is) after it is ready. jQuery provides a nice method to do just this. http://api.jquery.com/ready/
$(function() {
// Handler for .ready() called
});
Use the .html() function within jQuery to write to your <body> element or a <div> element that you write directly in your HTML file. http://api.jquery.com/html/
HTML
<html>
<body>
<div id="content"></div>
</body>
</html>
jQuery
var page = "This should have the HTML you want to insert";
$(function() {
$("#content").html(page);
});
I am unable to view new search results as they are updated in the database. Only using Internet Explorer 8. I tried refreshing the page but that does nothing. Chrome and Firefox work fine.
I am able to temporarily resolve this issue by choosing "Check for new versions of the stored page : Every time I visit the webpage". However, as I move this into production this means it won't work for end users using IE 8 9 or 10.
Any helpful tips are appreciated. Thanks in Advance.
Here is what I have already tried:
<script> type=“text/javascript" src="js/jquery-1.9.1.min.js?new=yes"></script>
$(document).ready(function () {
//http://stackoverflow.com/questions/217957/how-to-print-debug-messages-in-the-google-chrome-javascript-console/2757552#2757552
if (!window.console) console = {};
console.log = console.log || function () {};
console.dir = console.dir || function () {};
//listen for keyup on the field
$("#searchField").keyup(function () {
//get and trim the value
var field = $(this).val();
field = $.trim(field)
//if blank, nuke results and leave early
if (field == "") {
$("#results").html("");
return;
}
console.log("searching for " + field);
$.getJSON("cfc/test.cfc?returnformat=json&method=search", {
"search": field
}, function (res, code) {
var s = "<table width='1000' class='gridtable' name='table1' border='1'><tr><th width='40'>Attuid</th><th width='80'>Device</th><th width='55'>Region</th><th width='140'>Problem</th><th width='160'>Description</th><th width='120'>Resolution</th> <th width='180'>Resolution Description</th><th width='40'>Agent</th><th width='140'>Timestamp</th></tr>";
s += "";
for (var i = 0; i < res.table_demo.length; i++) {
s += "<tr><td width='42'>" + res.table_demo[i].pa_uid +
"</td><td width='80'>" + res.table_demo[i].pa_device +
"</td><td width='55'>" + res.table_demo[i].pa_region +
"</td><td width='140'> " + res.table_demo[i].pa_problem +
"</td><td width='160'> " + res.table_demo[i].pa_description +
"</td><td width='120'>" + res.table_demo[i].pa_resolution +
"</td><td width='180'>" + res.table_demo[i].pa_rdescription +
"</td><td width='42'> " + res.table_demo[i].pa_agent +
"</td><td width='140'> TimeStamp"
"</td>";
s += "</tr>";
}
s += "</table>";
$("#results").html(s);
});
});
})
Try $.ajaxSetup({ cache: false }); so disable jQuery caching.
Also to expire pages instantly, try adding meta tags:
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1" />
I have written JavaScript outside the <body> end tag. Firebug is unable to detect the JavaScript and I am unable to detect the JavaScript error.
Here is my code:
</body>
<script type="text/javascript">
function displayIFrameContent()
{
var iFrame = document.getElementById("link");
var if1= "<iframe src='http://leadmarket.hol.es/forms/solar-power.php?adv_id=" + <?php echo($fetch_users_data['id']); ?>;
var if2= "<iframe src='http://leadmarket.hol.es/forms/kitchen-installation.php?adv_id=" + <?php echo($fetch_users_data['id']); ?>;
var if3= "<iframe src='http://leadmarket.hol.es/forms/conservatory.php?adv_id=" + <?php echo($fetch_users_data['id']);; ?>;
var host = document.getElementById("host");
var subId = document.getElementById("subid");
var errorClass = "box form-validation-error border-width-2";
if(host.value == "")
changeClass("host", errorClass);
if(host.value != "")
{
var iFrameEnd = " width='280' height='330' frameborder='0' scrolling='no'></iframe>";
var leadTypeSelect = document.getElementById("leadType");
var leadTypeValue = leadTypeSelect.options[leadTypeSelect.selectedIndex].value;
iFrame.value = "";
if(leadTypeValue == 1)
iFrame.value = if1 + "&" + "sub_id=" + subId.value + "&source=" + host.value + "'" + iFrameEnd;
if(leadTypeValue == 2)
iFrame.value = if2 + "&" + "sub_id=" + subId.value + "&source=" + host.value + "'" + iFrameEnd;
if(leadTypeValue == 3)
iFrame.value = if3 + "&" + "sub_id=" + subId.value + "&source=" + host.value + "'" + iFrameEnd;
}
}
function changeClass(id, classname)
{
document.getElementById(id).setAttribute("class", classname);
}
</script>
</html>
Your help will be appreciated. Thanks in advance!
The code above contains PHP tags, which cause some syntax errors. In this case the Script panel just shows a message "No Javascript on this page". You should make sure they are interpreted by PHP before they are output to the browser.
Also JavaScript syntax errors are listed within the Console Panel. Make sure you have the option Show JavaScript Errors checked to see them.
Furthermore there's a detailed description about script debugging available within the Firebug wiki.
Sebastian
I am having issues passing two coordinates from one function to another. I don't really know JavaScript, but it seems to be somewhat correct. Could you please let me know where is my error?
<head>
<script>
var zoom = 12; // 18 for mobile phones because the geolocation is more accurate
function init() {
// Don't bother if the web browser doesn't support cross-document messaging
if (window.postMessage) {
if (navigator && navigator.geolocation) {
try {
navigator.geolocation.getCurrentPosition(function(pPos) {
send(pPos.coords.latitude, pPos.coords.longitude);
}, function() {});
} catch (e) {}
} else if (google && google.gears) {
// Relevant if targeting mobile phones (some of which may have Google Gears)
try {
var geoloc = google.gears.factory.create("beta.geolocation");
geoloc.getCurrentPosition(function(pPos) {
send(pPos.latitude, pPos.longitude);
}, function() {});
} catch (e) {}
}
}
}
function send(pLat, pLng) {
var myiframe = document.getElementById("myiframe").contentWindow;
// The third parameter, zoom, is optional
myiframe.postMessage(pLat + "," + pLng + "," + zoom, "http://www.qib.la");
}
window.onload=init;
</script>
</head>
<body>
<iframe id="myiframe" src="http://www.qib.la/embed/" width="400" height="400">
Check the prayer direction towards the Ka'ba in Makkah at
Qibla Direction.
</iframe>
<script type="text/javascript" src="http://praytimes.org/code/v2/js/PrayTimes.js"></script>
<br>
<p align="center">Waterloo, ON, Canada<p>
<div align="center" id="table"></div>
<script type="text/javascript">
var date = new Date(); // today
var times = prayTimes.getTimes(date, pLat + "," + pLng, -5);
var list = ['Fajr', 'Sunrise', 'Dhuhr', 'Asr', 'Maghrib', 'Isha', 'Midnight'];
var html = '<table id="timetable">';
html += '<tr><th colspan="2">'+ date.toLocaleDateString()+ '</th></tr>';
for(var i in list) {
html += '<tr><td>'+ list[i]+ '</td>';
html += '<td>'+ times[list[i].toLowerCase()]+ '</td></tr>';
}
html += '</table>';
document.getElementById('table').innerHTML = html;
</script>
</body>
Is it possible to return a value from an asynchronous function? How do I use a callback here?
You have a syntax error in this code:
var times = prayTimes.getTimes(date, + b + ',' + c + , -5);
You have a "+" and then a comma immediately following it. I'm not sure what your intent for the code was, but that is what is causing it to not run.
Perhaps you intended to follow the pattern of appending the commas as strings?
var times = prayTimes.getTimes(date + ',' + b + ',' + c + ',' + -5);
Or perhaps that -5 was meant to be a separate argument?
var times = prayTimes.getTimes(date, b + ',' + c, -5);