We help IT Professionals succeed at work.

How to create a javascript snippet to place in a website?

Medium Priority
416 Views
Last Modified: 2013-11-07
I am using Visual Studio and or Dreamweaver to create some basic web pages (I am a beginner) and have noticed on several test sites that I am working on the abliltiy to embed a Javascript snippet in a page - that calls the HTML.  

What I am looking for is how do you create the src part of the file below?  I can use visual studio or Dreamweaver.   I do know VB, but not Java?

Any tutorials or suggestions?
<script language="javascript" src="http://yada..." type="text/javascript"></script>

Open in new window

Comment
Watch Question

Top Expert 2005

Commented:
It's just a separate javascript file on your webserver somewhere.  You usually don't see an absolute URL here, but a relative path.  For instance, if you have a directory with your .html file in it, and you created a javascript file "myscripts.js" in the same directory, you would link like this.

<script language="javascript" src="myscripts.js" type="text/javascript"></script>

It's really no different than linking to other .html files on your webserver.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
The Javascript (not Java, by the way), in the specified location, or in the
specified file, is a collection of javascript statements.  So, it could be
something as simple as:

so,

<script src="yada.js" type="text/javascript"></script>

would be the same as:

<script src="yada.js" type="text/javascript">
function init() {
  alert( 'Hi' )
}
</script>

function init() {
  alert( 'Hi' )
}

Open in new window

with vb
ClientScript.RegisterClientScriptInclude("KeyName", "http://yada...")
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
sorry, the second should have been:

<script type="text/javascript">
function init() {
  alert( 'Hi' )
}
</script>

Author

Commented:
Thanks.   I do not know javascript.    I want to create a small html page (like a panel) with Visual Studio that I can embed in another site (I could use an iframe).   But, would like to learn how to do this with the javascript example in the original post.     I know how to create the HTML that I want.   The question is how to I then create a script to place this HTML in another site?

Thanks again.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
What, exactly, do you mean when you say:

create a script to place this HTML in another site
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
There are ways, in javascript, to completely manipulate the page on which the script is executing.  For example, the script could traverse the page description, and add, change, or delete individual document elements.

So, I'm not quite sure what it is you are trying to do.

Author

Commented:
Honorgod...

I have some HTML that I want to place in various sites.

I just want the javascript to return/echo some html like in this example...

"<p>Hello Everyone</p>"
"<p><img src="../mcdonaldsLogo.jpg" width="111" height="101" alt="test" /> </p>"

How would the script read?

<script src="myscript.js" type="text/javascript">
function init() {
--->  write or echo the HTML that I want
}
</script>

Then use a line like this to call it ....

<script language="javascript" src="myscripts.js" type="text/javascript"></script>



HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
ok, that's a good start.

Where on the page would you like this placed?

Author

Commented:
It is going to be different on different pages.   But always within the body tags...  I can adjust for that once I figure out how to generate the script.

Thanks for you help.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Something like this perhaps?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<script type='text/javascript'>
  function insert( id ) {
    var ele = document.getElementById( id )
    if ( ele ) {
      ele.innerHTML = '<p>Hello Everyone</p><p><img src="../mcdonaldsLogo.jpg" width="111" height="101" alt="test" /></p>'
    } else {
      alert( 'Specified element not found.  id="' + id + '"' )
    }
  }
</script>
</head>
<body onload='insert("here")'>
<div id='here'>
</div>
</body>
</html>

Open in new window

Software Engineer
CERTIFIED EXPERT
Commented:
All you have to do is to locate the element after which you want the code
inserted, then assign the objectName.innerHTML value like shown above.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
The example that I am trying to emulate is this...
<script language="javascript" src="http://www.getzee.com/ocmarathon/travel.js" type="text/javascript"></script>

I have this code embedded on a website and it works perfectly.   I can place it anywhere that I need it.

My question what is the code in the travel.js?  This travel.js is just an example that and I want to be able to create a variety of scripts.   Maybe I am in over my head, but was hoping to take a page that I created, wrap in in javascript and then use this type of line for other sites to call.

Thanks very much.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
You are very welcome.  Thank you for the grade & points.

Good luck & have a great day.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.