0

Do any one have similar experience? I would like to load an external javascript dynamically while I click on a specific page. Below is the code I wrote:

    $("#tab-4-content").load("/reg.php?id="+this.id+"&ads="+$(this).data('ads')+"&f="+$(this).data('file')+"&mid="+$(this).data('mid'));
$.ajax({
  url: "http://www.mydomain/validation.js",
  dataType: "script",
  cache: false,
    }).done(function() {
        console.log("success load livevalidation");
    });
    $.mobile.changePage("#tab-4");
}); // tab-4-content click

validation.js is the script to valid the form input. When the cache setting is true, then the script seems not to be loaded successfully because an error prompted in Chrome Javascript Console. After I change it to false, it works.

The problem is if I keep the browser on without clicking the #tab-4-content for a day, I tried the click it after the day, the error prompted again, then I have to change the cache to true, execute one and then false again, it resume normal. I have no idea how the cache do, is there time expire issue and the error is caused by the cache setting?

Error prompted: Uncaught ReferenceError: Validation is not defined

Best regards,

Kelvin

6
  • 1
    Where is the variable reference Validation being called and/or defined? If I'm reading this correctly the browser is complaining about a runtime error in validation.js itself and yet your not providing that code. So how can anyone help with this if they can't even see the problem? Commented Jul 14, 2014 at 2:33
  • What's the problem with just adding a script element dynamically? Commented Jul 14, 2014 at 2:33
  • @FelixKling I'm pretty sure that is exactly what .getScript does. Commented Jul 14, 2014 at 2:34
  • The script can be called but not stable. Yes, will study the .getScript Commented Jul 14, 2014 at 3:10
  • I dont really get why your loading in the library like this, and not like @Jasons answer mentions. If this is for form validation, why not just include a script tag to pull in the js. And you dont have to worry about all of this to make it available. Commented Jul 14, 2014 at 23:20

2 Answers 2

2

this is .getScript function, you can try this

$.getScript( "http://www.mydomain/validation.js" )
.fail(function() {
    // so something
}).done(function() {
    console.log("success load livevalidation");
});

but i do think the error is on validation.js itself like Sukima said, check on that file first if this doesn't work.

Sign up to request clarification or add additional context in comments.

Comments

0

Bypass all the extra coding. Just create a script tag and source to the javascript file you need in the page you are loading.

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

2 Comments

<script></script> doesn't work for loading script dynamically.
That what I was getting at though. If he is just validating a form, why the need and extra overhead to validate dynamically like this. I know <script> doesnt work with loading dynamically. I am just wondering why it's being done dynamically in the first place, rather that just in a script tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.