1

I can't get these simple things to work, I have an text file with some text in it and I want an alert containing the text thats inside of a text file to be triggered when clicking a "btn/link".

Also I want to be able to compare and see if the text inside the file is true or false:

JSFIDDLE demo

HTML:

<div id="alert_btn"><a href="javascript:alert_fn();">click_me</a></div>

JQuery/javascript:

function alert_fn(){
    $.get('http://www.patan77.com/example_test.txt', function(data) {
        alert(data);
        if (data == true){
            alert("true");
        }else{
            alert("false");
        }
    });   
}

Thanks in advance.

Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
  • 1
    cross domain request. No 'Access-Control-Allow-Origin'. – Milind Anantwar Sep 07 '15 at 13:57
  • data is not of boolean type so **if (data == true){** cannot be true. you can check **if(data)** for checking the response is not a null value. For cross domain ajax request refer http://stackoverflow.com/questions/16989505/jquery-cross-domain-ajax – Mayank Sep 07 '15 at 14:00
  • Found that in Chrome DevTools (f12) then under network its possible to disable cache (the DevTools need to be open when refreshing the page for it to work). – Patrik Fröhler Sep 07 '15 at 15:10

1 Answers1

1

Think I got it working now.

Test_DEMO

HTML with jQuery / Javascript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="1" />
<title>Test_Alert</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>

function alert_fn(){
    $.get('http://www.patan77.com/example_test.txt', function(data) {
       alert(data);
    });   
}


function alert_fn_2(){
    $.get('http://www.patan77.com/example_test.txt', function(data) {
        var text_file = data;

        if (text_file == "hello world"){
            alert ("its true");
        }else{
            alert ("its false");
        }

    });
}

</script>
</head>

<body>
<div id="alert_btn"><a href="javascript:alert_fn();">click_me</a></div>
<div id="alert_btn_2"><a href="javascript:alert_fn_2();">click_me_to_see_if_true</a></div>
v.06
</body>
</html>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38