0

I have the following code. It works in an html document. I would like to move the script in the head to an external .js document. Still very new at this and so far everything I've seen has to do with functions, but I want to assign the variables in the .js doc so they're not viewable in the html. Any help would be appreciated. Thanks!

<html>
<head>
<script type="text/javascript">
var userName = "exampleaddress";
var emServer = "exampledomain.com";
var tagLine = "?subject=Questions/Comments Re: exampledomain.com";
var emLink = userName + "@" + emServer + tagLine;
</script>
</head>

<body>
<script>
document.write("<a href='http://www."+emServer+"'>");
document.write(emServer);
document.write("<a>");
</script>
<br>
<script>
document.write("<a href='mailto:"+emLink+"'>");
document.write(userName);
document.write("<a>");
</script>
</body>
</html>
Scott
  • 15
  • 5

3 Answers3

2

Moving it to an external file isn't going to stop the users from being able to see it. If the browser has access to something, so does the client.

Similar to an image, all you would need do is put the javascript in an external file and link to it.

Such as:

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

I don't know why, but script tags can't be self-contained.

mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • and placing the script in the head in another file: but then I see nada in the browser. Using IE and FF. – Scott Mar 14 '12 at 00:53
  • `jqu/jquery-1.6.2.js` will look for a folder called `jqu` that's in the same directory as the webpage. If you want it to look for `jqu` in the root directory, use `/jqu/jquery-1.6.2.js`. Also, check out http://stackoverflow.com/questions/2180391/why-should-i-use-googles-cdn-for-jquery – mowwwalker Mar 14 '12 at 00:59
1

Move everything to an external js file and load it like:

<script src="js/script.js"></script>

Then create empty containers and instead of document.write you'll have to use innerHtml.

var userName = "exampleaddress",
    emServer = "exampledomain.com",
    tagLine = "?subject=Questions/Comments Re: exampledomain.com",
    emLink = userName + "@" + emServer + tagLine;

var html = '<a href="http://www."'+ emServer +'">' + emServer + '<a>';
document.getElementById('container').innerHtml = html;

//...
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • That's a bit over my head right now. How would I link to that to get the results to appear in the html doc? I haven't found my innerHtml yet :) – Scott Mar 14 '12 at 01:03
0

To achieve what you have asked:

<head>
    <script type="text/javascript" src="past/to/file.js"></script>
</head>

file.js contains:

var userName = "exampleaddress";
var emServer = "exampledomain.com";
var tagLine = "?subject=Questions/Comments Re: exampledomain.com";
var emLink = userName + "@" + emServer + tagLine;

EDIT:

The part of your question that isn't possible is having the variables not visible. HTML ans JS files are always accessible in plain text (for lack of better words)

If you are trying to hide these values from the end user, it is impossible in the way you have asked. Tell us your true purpose and we might be able to propose a better solution.

bPratik
  • 6,894
  • 4
  • 36
  • 67
  • I'm trying to avoid spam from email harvesters. I thought having the address "disected" in a jquery file would take care of most of that. – Scott Mar 14 '12 at 00:56
  • Thanks! I was trying to use script tags in the .js doc. D'oh! Working now!!! Is it okay to ask about your ideas for hiding the email address? – Scott Mar 14 '12 at 01:01
  • Great! If it is an attempt to avoid harvesting, you will find a load of info in: http://stackoverflow.com/a/4141026/476786 and http://stackoverflow.com/a/4184584/476786 . If you ask me, I would never put the address on the webpage, instead I would include a contact form that people could contact me via. – bPratik Mar 14 '12 at 01:09
  • I have a contact form, but thought I should be putting a webmaster email address on the site. – Scott Mar 14 '12 at 01:39
  • 1
    Thank you for the suggestion. I have been looking into some sort of encryption even in a rudimentary way. That will take more time, as my skills are pretty green, but I'd rather start off with some protection than realize there's a problem when my inbox is at capacity. – Scott Mar 17 '12 at 00:05