/    Sign up×
Community /Pin to ProfileBookmark

How do I…

I’m not experienced with javascript and don’t even know if this is possible, but I’d like a situation where I have a certain line or two of javascript code that is not seen by the browser upon initial loading of the page that can then be incorporated into that page (ie the page reloaded with that previously hidden code) by clicking a button (or something analogous).

My initial idea was to wrap that code in curly brackets and assign a function to it so it wouldn’t be read unless I called upon the function. But I don’t know if functions can insert previously unread code into the source itself or how to execute them properly if they can (my attempts have failed to yield a page that actually read the code wrapped in that function). I can be more specific about exactly what I’m trying to do if it helps, but that’s basically it.

Thanks for the help in advance ?,

Outer

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@brendandonhueMay 04.2003 — You could probably use cookies to decide if the page has been viewed before or not.

But if you want to put the code in a function and call it using a link it would look like this.

<script language="javascript">

function MyFunction()

{

blah blah blah

}

</script>

To have a link that executes it

<a href="javascript:MyFunction()">Click here</a>
Copy linkTweet thisAlerts:
@OuterauthorMay 05.2003 — Right,

This is essentially what I did (though I used a button instead of a link). But clicking the button didn't cause the page to change any. I figured I wanted the page to reload with the new code in it, so I added a history.go(0) to my function. Now when the button is clicked the page reloads, but that code I want in there still isn't being read.

So, I guess what I'm getting at is: how do I get the page to initially load without the "blah blah blah" in the source, and then be able to add it in with the touch of a button?

Outer
Copy linkTweet thisAlerts:
@brendandonhueMay 05.2003 — If you want a webpage to do something, you have to put it in the source. The browser can't magically decide what you want it to do.

Why does it matter if its in the source or not, the user won't see it unless they go looking or hit the link.

You could put it in an external js file but I don't know why you would do that.
Copy linkTweet thisAlerts:
@ScriptageMay 05.2003 — First of all put the code in an external js called whatever.js...then

<script>

if(!document.cookie){

date = new Date;

date.setMonth(date.getMonth()+1);

document.cookie = "name=value; expires = " + date.toGMTString();

}else{

expired = new Date;

expired.setMonth(expired.getMonth()-1);

document.cookie = "name=x; expires = " + expired.toGMTString();

document.write("<script src='whatever.js'></script>");

}

function reload_win(){

location.reload();

}

</script>

<a href='javascript:reload_win()'>Add code</a>
Copy linkTweet thisAlerts:
@OuterauthorMay 06.2003 — I did what you suggested, however it didn't really do anything. I got the sense all it did was reload the page.

I'm very new to this, so do you think you could tell me what each piece of your code does and how it works? That may help me modify it to what I need, or spark other ideas about how I can fix my problem.

Otherwise, I can actually provide you with some of my code so my problem becomes clearer to you. In any case, thank you for the help.

Outer
Copy linkTweet thisAlerts:
@OuterauthorMay 06.2003 — Oh, and Brendandonhue, I guess I could be a little clearer about what I'm trying to get at. See, the idea isn't for someone else to be able to see the code I'm putting in there. Instead, my page is built so that two particular lines of code will drastically change everything on the page. I tested this, and the two lines of code I want (if put in the right place in the source) will work just fine. But, I only want this change to occur if the viewer of the page so chooses (as in they press a button or hit a link or something). Otherwise, I just want the page to load as if that extra code didn't exist (I want the default to be minus that code). I can't figure out how to do this, though.

Again, if people want to actually see some of the code I can oblige.

Outer
Copy linkTweet thisAlerts:
@brendandonhueMay 06.2003 — Im confused.

Why wouldn't my code work for that? The code will not be executed until the user clicks the link.
Copy linkTweet thisAlerts:
@ScriptageMay 07.2003 — basically what happens is...

the script checks to see if there has been a cookie set, if there has been then a script object is written to the page with the src being the external js file with your two lines of code in, and then deletes the cookie. If a cookie has not been set a cookie is set.

The important part is here:

document.write("<script src='whatever.js'></script>");

Save your code as a .js file called whatever.js . This file is then put into the page if a cookie has been set.

There is an easier way to do this however. You could do it server side.

I can help you with this if you want.

Regards
Copy linkTweet thisAlerts:
@ScriptageMay 07.2003 — This is a file called page.pl

#!/usr/bin/perl -w

use strict;

use CGI qw(:all);

use CGI qw(fatalsToBrowser);

print header;

#########################################################

my $javascript_file = 'code.js';


my $page_file = 'index.html';

#########################################################


my (@javascript, @page) = '';

my $switch = param('switch');

open(PF, "$page_file") || die "Cannot open $page_file: $!";

@page = <PF>;

close(PF);

open(JSF, "$javascript_file") || die "Cannot open $javascript_file: $!";

@javascript = <JSF>;

close(JSF);

for(my $i=0; $i<scalar(@page); $i++){

if($switch eq '1' && index($page[$i],'<insert js>') > -1){

print<<JSDOC;

<script>

@javascript

</script>

JSDOC

}else{

print $page[$i];

}

}

This is your HTML file index.html

<html>

<head>

<insert js>

</head>

<body>

blah blah blah blah

blah blah blah

blah blah

blah

<a href='page.pl?switch=1'>Add code</a>

</body>

</html>

Note the <insert js> tag.

Place this tah where you want the javascript to be inserted into the document but it must be on it's OWN LINE, otherwise the rest of the line will be overwritten with the javascript code.

A file called code.js:

document.write('Hello World!');


You only need to define the following variables:

my $javascript_file = 'code.js';


my $page_file = 'index.html';

And make sure you have a link like this:

<a href='page.pl?switch=1'>Add code</a>

in your HTML file.

A working example of this can be found at http://btscl.com/cgi-bin/page.pl

BUT all files must be held in your CGI-BIN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Regards

Carl
Copy linkTweet thisAlerts:
@OuterauthorMay 08.2003 — Carl,

I'm not sure what I'm doing wrong. Your example works fine, so I tried to mimic it, but whenever I hit the add code button with mine it just loads the page.pl file as if the browser is trying to read it as a text file. Instead of displaying that Hello World and all the blah's, it displays all the code that went into the page.pl.

Is the cgi-bin folder something very specific? I named a folder on my desktop "cgi-bin" and put page.pl, index.html, and code.js in there. Does my problem have something to do with that?

Outer
Copy linkTweet thisAlerts:
@brendandonhueMay 08.2003 — CGI only runs on a server that supports CGI, not Windows.
×

Success!

Help @Outer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...