Page 1 of 1
What about JS guys?
Posted: Mon Feb 05, 2007 3:07 pm
by boba
Hey, everybody!
As I see there is a lot of php questions discussing here.
I have one question which is not related to php-coding, but it may be useful for some people.
Here is simple JS code which I am using to create new <script> tag in the document.
Code: Select all
scriptNode = document.createElement("script");
scriptElement = this.container.appendChild(scriptNode);
scriptElement.src = src;
Everything is ok with this code when it is executedin IE, FF and even Opera.
But it's not ok in Safari!!!
This technique (named jsON) is very helpful when you are going to create dynamic html page without AJAX.
The main point here is that browser executes JS source which is connected to page. In other words when I specify src for newly created srcipt node the code I'm specifying link to, will be executed immediately. And it true if IE, FF and Opera. But Safari has the bug here and I have no ideas here. I can't use AJAX because of browser security protection (AJAX to external host is impossible) and I can't use PHP for this (I can use only html and JS).
Sorry for specific English

and thanks for help!
Posted: Mon Feb 05, 2007 3:11 pm
by Luke
1) that isn't json
http://json.org/
2) AJAX to external host is possible
3) This is the PHP - Code forum, this should have been posted in "Client Side"
Posted: Mon Feb 05, 2007 3:15 pm
by boba
Sorry for off-topic I'll try to look there, thanks.
You have really interested me with this "AJAX to external host is possible".
Hope somebody in "Client Side" will help me.
Thanks again.
Posted: Mon Feb 05, 2007 3:17 pm
by Luke
no need to post a new thread, if the moderators want this in client side, they'll move it there. You should read up on AJAX. Anything that is accessible by means of http can be accessed with ajax. Search for ajax on these forums, it's been discussed a lot.
Posted: Mon Feb 05, 2007 3:24 pm
by boba
Sorry but I've just posted new thread in Client Side forum

I've read a lot of articles according to AJAX but everywhere I found that AJAX couldn't send request to external host because of security reasons, and even more. I've tried to create such request and olny browser where this works is IE.
Thanks for your advice.
Posted: Mon Feb 05, 2007 3:43 pm
by Luke
Posted: Mon Feb 05, 2007 3:51 pm
by boba
This article doesn't explain what I need. I know enough to create AJAX request. The problem is in requests to external hosts (for example Flickr service API).
Posted: Mon Feb 05, 2007 4:08 pm
by Luke
have you tried it%AC post what you tried
Posted: Mon Feb 05, 2007 4:22 pm
by boba
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Here is the code I am using to create several AJAX cycles. Everything is ok with it until I try to request external host.
[syntax="javascript"]/*
* CONFIGURATIONS
*
* urls: array of link for ajax
* timeouts: array of timouts for ajax
* handlers: array of handlers for ajax.onreadystatechange events
*/
var links = new Array(
{
"url": "http://cron.dev.advanware.com/test/date.php",
"timeout": 5000,
"handler": loadingHandler,
"method": "GET",
},
{
"url": "http://cron.dev.advanware.com/test/date.php",
"timeout": 10000,
"handler": loadingHandler2,
"method": "GET",
},
{
"url": "http://cron.dev.advanware.com/test/date.php",
"timeout": 20000,
"handler": loadingHandler3,
"method": "GET",
}
);
/*
* EXTENDED AJAX CLASS
*
* Initailizing default ajax obecj with additional params
*/
function AjaxObject(objectNumber)
{
this.object = this.sequence
(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
)
this.number = objectNumber;
}
AjaxObject.prototype.sequence = function()
{
for (var i = 0; i < arguments.length; i++)
try {return arguments[i]()} catch (e) {}
return false;
}
/*
* AJAX LOADER
*
* Simple class for loading URLs through AJAX
*/
function Loader(objectNumber,link)
{
this.busy = false;
this.url = link.url;
this.timeout = link.timeout;
this.handler = link.handler;
this.method = link.method;
this.objectNumber = objectNumber;
}
Loader.prototype.loadLink = function(link, handle, owningObject)
{
var request = new AjaxObject(this.objectNumber);
request.object.onreadystatechange = function()
{
if (request.object.readyState != 4) return;
if (handle) handle.apply(owningObject, [request.object.responseText, request.number]);
}
request.object.open(this.method, link, true, "", "");
request.object.setRequestHeader("Connection", "Close");
request.object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.object.send(null);
}
Loader.prototype.sequence = function()
{
for (var i = 0; i < arguments.length; i++)
try {return arguments[i]()} catch (e) {}
return false;
}
Loader.prototype.loadingCycle = function()
{
if (!this.busy)
{
this.busy = true;
var time = new Date().getTime();
var antiCacheAddon = '?anticache='+time;
this.loadLink(this.url+antiCacheAddon, this.handler);
}
setTimeout("loaders["+this.objectNumber+"].loadingCycle();", this.timeout);
}
/*
* LOADING HANDLERS
*
* These functions handle onReadyStateChange of ajax objects.
*/
function loadingHandler (result,objectNumber)
{
loaders[objectNumber].busy = false;
document.getElementById('time').value = result;
}
function loadingHandler2 (result,objectNumber)
{
loaders[objectNumber].busy = false;
document.getElementById('time2').value = result;
}
function loadingHandler3 (result,objectNumber)
{
loaders[objectNumber].busy = false;
document.getElementById('time3').value = result;
}
/*
* START LOADING
*/
var loaders = new Array();
function initLoading ()
{
for (var i = 0; i < links.length; i++)
{
loaders[i] = new Loader(i,links[i]);
loaders[i].loadingCycle();
}
}
/*
* PAGE ONLOAD LISTNERS
*/
if (window.addEventListener)
window.addEventListener("load", initLoading, false);
else if (window.attachEvent)
window.attachEvent("onload", initLoading);
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Tue Feb 06, 2007 3:53 am
by boba
No ideas on how to request external host?
The sample I've posted before creates several AJAX cycles. Each of them is executed with specified interval. Everything is ok with this script, except one: If I'll try to request any external host, I'll get security error in my browser. What can I do to resolve this issue?
Posted: Tue Feb 06, 2007 10:12 am
by feyd
Use your server as a proxy.
Posted: Wed Feb 07, 2007 5:41 am
by boba
2 feyd:
Do you mean to create some server-side script which will operate like proxy?
Unfortunately I can't use server-side scripts because of project requirements...
I just need to know is it possible to create request with some GET data to external host (using AJAX or not) without any server-side scripting.