Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2010, 06:53 PM   PM User | #1
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
JS Include and usage?

Hi I am in the middle of building some API's for my site and have built a cernal for all API's to read but i dont want the user to add two script tags to their sites code so I was wondering how I could do this.

These are not my real files but will give you the full view of what it is I am trying to do.

- cernal.js
Code:
var error = function(o) {
  if(o !== '') {
    alert(o);
  }
};
- pub109283746374.js
Code:
// include the cernal.js file here somehow
 
// Do a small function that uses the error cernal function
function ShowNotice() {
  if(API_KEY !== '' && API_KEY !== undefined) {
    alert('Success');
  } else {
    error('Sorry but you have an invalid API key.');
  }
}
- http://www.userswebsite.com/demoFile.html
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>WHOISearch API Demo</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="http://www.mysite.com/API/pub109283746374.js"></script>
</head>
<body>
  <script type="text/javascript">
  //<![CDATA[
    ShowNotice(); // Alerts 'Sorry but you have an invalid API key.'
  //]]>
  </script>
  <script type="text/javascript">
  //<![CDATA[
    API_KEY = 'pub109283746374';
    ShowNotice(); // Alerts 'Success'
  //]]>
  </script>
</body>
</html>
If anyone can help me it will help me so much with finishing these API's, please reply if you know how I can do this, Thank You.

DJCMBear
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 12-10-2010 at 07:25 PM..
DJCMBear is offline   Reply With Quote
Old 12-09-2010, 07:16 PM   PM User | #2
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Sorry for the mess here. See my other reply for correct answer.

Last edited by Shaka Zorba; 12-09-2010 at 08:21 PM..
Shaka Zorba is offline   Reply With Quote
Old 12-09-2010, 07:24 PM   PM User | #3
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Quote:
Originally Posted by Shaka Zorba View Post
// include the cernal.js file here with document.write
document.write('<script type="text/javascript" src="js2.js"></script>')
// Do a small function that uses the error cernal function
function ShowNotice() {
if(API_KEY !== '' && API_KEY !== undefined) {
alert('Success');
} else {
error('Sorry but you have an invalid API key.');
}
}
Thanks for you reply but I have tried that way and it didnt work, it just wrote to the body and didnt allow me to use the functions in the functions in the cernal file.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-09-2010, 08:07 PM   PM User | #4
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>WHOISearch API Demo</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="js1.js"></script>
</head>
<body>
    <script type="text/javascript">
  //<![CDATA[
    //API_KEY = 'pub109283746374';
    ShowNotice(); // Alerts 'Sorry but you have an invalid API key.'
  //]]>
  </script>
</body>
</html>
js1
Code:
document.write('<s');
document.write('cript type="text/javascript" src="cernal.js"></s');
document.write('cript>');


// include the cernal.js file here somehow
 
// Do a small function that uses the error cernal function
function ShowNotice() {
  if(typeof API_KEY != 'undefined') {
    alert('Success');
  } else {
    setTimeout("error('Sorry but you have an invalid API key.')",1);
  }
}
cernal
Code:
var error = function(o) {
  if(o !== '') {
    alert(o);
  }
};

Last edited by Shaka Zorba; 12-09-2010 at 09:07 PM..
Shaka Zorba is offline   Reply With Quote
Old 12-10-2010, 01:16 AM   PM User | #5
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
Code:
document.write('<s');
document.write('cript type="text/javascript" src="cernal.js"></s');
document.write('cript>');

You don't need to do all this, just properly escape the slashes with backslashes.

document.write('script type="text/javascript src="cernal.js"><\/script>');

If it goes through two javascript processes, you will need three backslashes, etc. In a javascript process for HTML all slashes should be escaped.

Last edited by DrDOS; 12-10-2010 at 03:53 PM..
DrDOS is offline   Reply With Quote
Old 12-10-2010, 11:44 AM   PM User | #6
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Quote:
Originally Posted by DrDOS View Post
[code]document.write('<s');
document.write('cript type="text/javascript" src="cernal.js"></s');
document.write('cript>');

You don't need to do all this, just properly escape the slashes with backslashes.

document.write('script type="text/javascript src="cernal.js"><\/script>');

If it goes through two javascript processes, you will need three backslashes, etc. In a javascript process for HTML all slashes should be escaped.

omfg thanks for the last bit of information that's the problem I was getting now I know how to stop the double loading I can now doo it to work with the cernal.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:37 AM.


Advertisement
Log in to turn off these ads.