What about JS guys?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

What about JS guys?

Post 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!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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"
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post 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).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

have you tried it%AC post what you tried
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post by boba »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Use your server as a proxy.
User avatar
boba
Forum Newbie
Posts: 15
Joined: Sun Feb 04, 2007 2:46 pm
Location: Kharkov, Ukraine
Contact:

Post 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.
Post Reply