/    Sign up×
Community /Pin to ProfileBookmark

Need help moving to external .js

I have a script that works fine, but I’m not sure how to move it to an external .js file.

Here is the script…

[code=html]
<script>
function passWord() {
var testV = 1;
var pass1 = prompt(‘ENTER PASSWORD (member name must also match records)’,’ ‘);
while (testV < 3) {
if (!pass1)
history.go(-1);
if (pass1.toLowerCase() == “member”) {
window.open(‘http://ww6.aitsafe.com/cf/add.cfm?userid=8579715&product=Discounted+Product&price=20.00’);
break;
}
testV+=1;
var pass1 =
prompt(‘Password Incorrect – Please Try Again.’,’Enter Password’);
}
if (pass1.toLowerCase()!=”password” & testV ==3)
history.go(-1);
return ” “;
}
</script>

<center>
<form>
<input type=”button” value=”Buy as Member” onClick=”passWord()”>
</form>
</center>
[/code]

What I want to do is move everything to an external file, except this part…

[code=html]http://ww6.aitsafe.com/cf/add.cfm?userid=8579715&product=Discounted+Product&price=20.00[/code]

My hope is that I can call that part from the html page itself (without having to retype the script multiple times). There will be lots of links like this on a single page (the only difference being the URL).

[B]Is this possible?
Or is there a better way to do this?[/B]

  • * P.S. I do realize that doing passwords via Javascript is totally insecure as all the buyer needs to do is view source. The client just wanted a mild deterrent and doesn’t care much whether it’s secure or not (as they check member purchases against records either way). Of course, if you DO know of a secure (but still simple) way to do this, I’m all ears! ?
  • to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @VictoriousMay 07.2011 — I have a script that works fine, but I'm not sure how to move it to an external .js file.

    Here is the script...

    [code=html]
    <script>
    function passWord() {
    var testV = 1;
    var pass1 = prompt('ENTER PASSWORD (member name must also match records)',' ');
    while (testV < 3) {
    if (!pass1)
    history.go(-1);
    if (pass1.toLowerCase() == "member") {
    window.open('http://ww6.aitsafe.com/cf/add.cfm?userid=8579715&product=Discounted+Product&price=20.00');
    break;
    }
    testV+=1;
    var pass1 =
    prompt('Password Incorrect - Please Try Again.','Enter Password');
    }
    if (pass1.toLowerCase()!="password" & testV ==3)
    history.go(-1);
    return " ";
    }
    </script>

    <center>
    <form>
    <input type="button" value="Buy as Member" onClick="passWord()">
    </form>
    </center>
    [/code]


    What I want to do is move everything to an external file, except this part...
    [code=html]http://ww6.aitsafe.com/cf/add.cfm?userid=8579715&product=Discounted+Product&price=20.00[/code]

    My hope is that I can call that part from the html page itself (without having to retype the script multiple times). There will be lots of links like this on a single page (the only difference being the URL).

    [B]Is this possible?

    Or is there a better way to do this?[/B]


  • * P.S. I do realize that doing passwords via Javascript is totally insecure as all the buyer needs to do is view source. The client just wanted a mild deterrent and doesn't care much whether it's secure or not (as they check member purchases against records either way). Of course, if you DO know of a secure (but still simple) way to do this, I'm all ears! ?
  • [/QUOTE]


    To obscure the code you could use http://marijnhaverbeke.nl/uglifyjs to obfuscate the code from the user.
    Copy linkTweet thisAlerts:
    @idesign123authorMay 07.2011 — Thanks for the suggestion. Here's the uglified version...
    [code=html]function passWord(){var a=1,b=prompt("ENTER PASSWORD (member name must also match records)"," ");while(a<3){b||history.go(-1);if(b.toLowerCase()=="member"){window.open("http://ww6.aitsafe.com/cf/add.cfm?userid=8579715&product=Discounted+Product&price=20.00");break}a+=1;var b=prompt("Password Incorrect - Please Try Again.","Enter Password")}b.toLowerCase()!="password"&a==3&&history.go(-1);return" "}[/code]

    The password is still pretty visible, but that's ok. Security isn't the main concern. What I'm mainly wondering is if there is a way to reuse the code for multiple buttons.

    My ideal scenario would be if I could have the script only once in the .js code, and move the destination URLs to the body area. I've seen this done with popup window code (i.e., most of the code is in the header and just the destination URL is in the body area), but I'm still pretty new to Javascript so I'm not sure how to convert my code to this type of format.

    Anyone here know how to do this?
    ×

    Success!

    Help @idesign123 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 6.16,
    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: @nearjob,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...