Current location: Hot Scripts Forums » General Web Coding » JavaScript » Loading an external file


Loading an external file

Reply
  #1 (permalink)  
Old 06-05-09, 01:35 PM
vitron's Avatar
vitron vitron is offline
Newbie Coder
 
Join Date: Mar 2009
Location: Bradford
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Post Loading an external file

Hey guys, its me again
Im having another problem, I want to be able to load an external file, quick and easy, through anywhere in my javascript, to anywhere in my document.
Heres the code I made, should work with any image/<acronym title="JavaScript"><acronym title="JavaScript">js</acronym></acronym>/css/xml files:
Code:
// @htmlTag = html tag of file type loading
// @filename = file name to load, its location as well, duh!
// @extraAttributes = any extra attributes in an array [OPTIONAL]
// @where to append the loaded file [OPTIONAL]
loadfile(htmlTag, filename, extraAttributes, appendTo) {
	var fileref = document.createElement(htmlTag);
	if (htmlTag === 'link' || htmlTag === 'a') {
		fileref.setAttribute("href", filename);
	} else {
		fileref.setAttribute("src", filename);
	}
	if (extraAttributes !== undefined || extraAttributes !== 0 || extraAttributes !== false) {
		for (key in extraAttributes) {
			if (key !== 0) {
				fileref.setAttribute(key, extraAttributes[key]);
			}
		}
	}
	if (typeof fileref !== undefined) {
		if (appendTo === undefined || appendTo === 0 || appendTo === false || appendTo === 'body') {
			document.getElementsByTagName("body")[0].appendChild(fileref);
		} else {
			if (appendTo === 'head') {
				document.getElementsByTagName("head")[0].appendChild(fileref);
			} else {
				appendTo.appendChild(fileref);
			}
		}
	}
}
After making the above code, I decided that I want to make default functions for each file type that can be loaded into a html document, so I started of with these two:
Code:
loadimage(filename, altAttr, moreAttr) {
	if (altAttr === undefined || altAttr === 0 || altAttr === false) {
		altAttr = filename;
	}
	if (moreAttr === undefined || moreAttr === 0 || moreAttr === false || moreAttr.constructor !== Array || moreAttr.constructor !== Object) {
		moreAttr = [];
	}
	moreAttr["alt"] = altAttr;
	loadfile('img', filename, moreAttr);
}
loadjs(filename, moreAttr) {
	if (moreAttr === undefined || moreAttr === 0 || moreAttr === false || moreAttr.constructor !== Array || moreAttr.constructor !== Object) {
		moreAttr = [];
	}
	moreAttr["type"] = "text/javascript";
	loadfile('script', filename, moreAttr);
}
// loadimage('http://vitronprince.com/image_gallery/images/vitron_prince.jpg');
// loadjs('http://vitronprince.com/image_gallery/imageflow.<acronym title="JavaScript">js</acronym>');
Now, with the scripts I made, i can load a image perfectly, but when I tried the loading an external javascript file, it didnt work. I been trying to work this problem for over 7+ hours and no luck.

Maybe someone can try and run these scripts, and see if it works for you, and help me fix my problem
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
files, javascript, load, remote


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problems linking to external CSS from my XHTML file gej03 HTML/XHTML/XML 15 03-10-09 08:20 PM
Textfield, CSS stylesheet external loading Flash_Boi Flash & ActionScript 1 03-03-09 07:43 AM
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 06:23 AM
displaying image while loading external file UnrealEd JavaScript 5 09-24-06 06:43 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 04:27 PM


All times are GMT -5. The time now is 01:17 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.