-2

I just created a HTML file and that file has a link that goes to the server and retrieves the value that is a number.

I am just wondering that can we have that value that is generated by link in that alert box.

To be clear I dont want link in alert box but I want to display the value that is returned from the server

See here my code

<!DOCTYPE html>
<html>
<body>
    <p>Click the button to display an alert box:</p>
    <button onclick="myFunction()">Try it</button>
    <script>
        function myFunction() {
            alert("I am an alert box!");
        }
    </script>
    <a href="http://localhost:8080/TemperatureReading/"> test </a>
</body>
</html>

Here alert box is saying-

I am an alert box!

Now, what I want is can I have value showing in alert box which is the value from the server( the value which is in the link).

This can be very easy but I am not able to get it. Can anyone help me on this issue. Thank you in advance!

EDIT: I have also tried ajax GET request--

<!DOCTYPE html>
<html>
<body>
    <h2>AJAX</h2>
    <button onclick="myFunction()">Try it</button>

    <script>
        function loadDoc() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById("demo").innerHTML = xhttp.responseText;
                }
            };
            xhttp.open("GET", "http://localhost:8080/TemperatureReading/", true);
            xhttp.send();
        }
    </script>
</body>
</html>

But where to have alert??

shv22
  • 680
  • 5
  • 28
  • what is the link value? are your referring the href or the test? – brk May 17 '16 at 07:26
  • you need which value to be alerted? – Anoop Joshi P May 17 '16 at 07:26
  • @user2181397 it is numeric...the test is href – shv22 May 17 '16 at 07:28
  • @AnoopJoshi the value which is in link test I want it in Alert box – shv22 May 17 '16 at 07:28
  • where are you seeing value in link test. Please write the value – brk May 17 '16 at 07:29
  • I think he going inside that link and he is finding a number and he want to display that number inside his alertbox!i dont think its possible without ajax or retrieving value from json. – Shrikantha Budya May 17 '16 at 07:35
  • [demo](https://jsfiddle.net/mxtvnd6a/) check event handler – guradio May 17 '16 at 07:37
  • @guradio it will have the link in alert box...I want value that link has in laert box – shv22 May 17 '16 at 07:42
  • @ShubhamVashishtha you are confusing with your question and comments. What do you want in alert?? do you want the alert to display **`http://localhost:8080/TemperatureReading/`** as it is in the `href` of the anchor tag **OR** do you want to **display the value that is returned from the server** when it hits the link ? – Rajshekar Reddy May 17 '16 at 07:42
  • @Reddy I want display the value that is returned from the server – shv22 May 17 '16 at 07:43
  • @ShubhamVashishtha you need to use Ajax then.. – Rajshekar Reddy May 17 '16 at 07:48
  • @Reddy I used it also...I have edited question please see – shv22 May 17 '16 at 07:50
  • @ShubhamVashishtha check my answer... – Rajshekar Reddy May 17 '16 at 07:54
  • @Reddy ...I commented in your answer – shv22 May 17 '16 at 07:57
  • @Jamiec I edited the question please see and remove duplicate tag – shv22 May 17 '16 at 07:58
  • What do you get if you type `http://localhost:8080/TemperatureReading/` into your browser? Why in the second example did you change that to `localhost://TempratureReading/JavaResources/src/ReadingController` (which is a almost certainly invalid URL)? – Jamiec May 17 '16 at 08:21
  • @Jamiec I will get a random number generated by server...sorry about edit I changed the file path in that – shv22 May 17 '16 at 08:33
  • Ok, so in your second example, did you see that random number in `

    ` element?

    – Jamiec May 17 '16 at 08:38
  • @Jamiec In hurry I just not look at that... again sorry – shv22 May 17 '16 at 09:25
  • @ShubhamVashishtha huh? That didnt answer my question! – Jamiec May 17 '16 at 09:29
  • @Jamiec yes man I see that...it is previous code when I am not able to achieve this thing...Now, when I achieved it I wanted to do this alert thing... – shv22 May 17 '16 at 09:34
  • So the second bit of code works, and you know how to show an `alert` but you cannot join the dots to get an alert instead of writing to `

    `? That is incredible! I think you're just after someone to hold your hand!

    – Jamiec May 17 '16 at 09:36
  • @Jamiec sorry man but its not like that....I am just under a gun I have been working on this for couple of days...I think I am eager to get answer without thinking what I am doing...sorry – shv22 May 17 '16 at 09:38
  • @Jamiec please see this http://meta.stackoverflow.com/questions/310160/so-chat-no-upload-button/310161#310161....that is why I am not able to see the button... – shv22 May 17 '16 at 10:47

5 Answers5

1

If you want to get href value of test and alert it on clicking button , try this:

function myFunction() {
   var href = $("a").attr("href");
  alert(href);
}
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
1

As you mentioned you want to display the value returned from the server this is what I suggest you.

  • Add the Jquery reference to your page. (I have added from CDN below)
  • Use Jquery to do a Ajax call to the link in your anchor tag.
  • On this Ajax success method display this returned value in a alert

Sample code below.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
  </head>
<body>

<p>Click the button to display an alert box:</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction() {    
 $.get($('#ajaxedAnchor').attr('href'),function(data){  // shot form of ajax get syntax
    alert(data);                                        // alert the returned data from server
 });     
}

</script>
<a href="http://localhost:8080/TemperatureReading/" id="ajaxedAnchor"> test </a>
</body>
</html>
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
  • Not coming again...but again if I click on link value is showing – shv22 May 17 '16 at 07:57
  • if you click on the link what is showing?? Can you give more details? – Rajshekar Reddy May 17 '16 at 08:01
  • If I click on link it just give me number thats it...and I want that number in alert box – shv22 May 17 '16 at 08:04
  • what is your server side language? – Rajshekar Reddy May 17 '16 at 08:07
  • The number is coming from random generator function and this function is written in JAVA – shv22 May 17 '16 at 08:08
  • @ShubhamVashishtha I dont see any error in my code... can you do one help.. copy the contents of the `myFunction()` and paste it in your browser console and execute it... let me know what happens – Rajshekar Reddy May 17 '16 at 08:14
  • Object { readyState: 1, getResponseHeader: .ajax/x.getResponseHeader(), getAllResponseHeaders: .ajax/x.getAllResponseHeaders(), setRequestHeader: .ajax/x.setRequestHeader(), overrideMimeType: .ajax/x.overrideMimeType(), statusCode: .ajax/x.statusCode(), abort: .ajax/x.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 8 more… } GET XHR http://localhost:8080/TemperatureReading/ – shv22 May 17 '16 at 08:26
  • @ShubhamVashishtha I asked you to execute the content which I have provide in my answer. :) that is this code `$.get($('#ajaxedAnchor').attr('href'),function(data){ alert(data); });` – Rajshekar Reddy May 17 '16 at 09:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112133/discussion-between-shubham-vashishtha-and-reddy). – shv22 May 17 '16 at 09:27
0

You can use the text() method to get the text of an element, which can be used inside the alert.

function myFunction() {
  alert($("a").text());
}

Seems like you haven't referred jquery in your code.

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
-1

Make POST or GET request to http://localhost:8080/TemperatureReading/ and return variable alert in your page

https://api.jquery.com/jquery.post/

snex
  • 982
  • 11
  • 21
-2

The following code requires jQuery:

function myFunction() {
    $.ajax({
        type:“GET”,
        url:"server address",
        success:function(data){//data is server print 
            alert(data)
        }
    })    
}
Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
Shinhwa
  • 1
  • 1
  • I tried this and replaced myFunction with your myFunction but now alert box is not working – shv22 May 17 '16 at 07:47