I want to writ a jscript code that asks user to type (example) his name and than that name to be stored in variable x.

this is what i have done so far, i am very new on jscript se please somebody can correct me with this.

var x = prompt('enter your name'){
    alert('hello'+ x)

correct me please

and second question is about the confirms, example how to ask user like if he want to delete something. any piece of small code any help or any website with good tutorials.

Recommended Answers

All 11 Replies

var x = prompt('enter your name');
    alert('hello '+ x);

Thnaks very much and what about the second question can you help me on that.

<input type="button" onclick="confirm('Are you sure you want to delete');" value="Display Example" />

What is the question?

My next question is how to do a ajax request on a specific URL. and the getting result to be stored in a div tag.
sorry about this question i know they are very easy but ia m very new in javascript and ajax

Ajax is easy to work with natively in JavaScript, but even eaiser if you leverage the jQuery library. Since you are new to JavaScript, I can assume you haven't been exposed to jQuery yet.

What you would do is create a JavaScript function that takes care of the Ajax request. Something like this..

function getContent() {
        var xhr = false;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } 
        else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xhr) {
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("div-1").innerHTML = xhr.responseText;
                }
            }
            xhr.open("GET", "targetFile.txt", true);
            xhr.send(null);
        }
    }

In this example, you are loading from a text file and placing the results in a div with an ID of "div-1"

Source examples: Ajax Examples

Fantastic Sir, thanks a lot, just curious to see how would it be with Jquery, also read some article and with was prefered to do qith jquery, can you show me how to do with jquery this example.

Its even easier to do with jQuery, but if you are planning to work with web development, its a good idea to really learn JavaScript before working with one or more of the common libraries such as jQuery. Otherwise, you'll spend most of your time searching for snippets here and there rather than writing your own custom code. In any event, here is the equivalent in jQuery..

<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<button id="getData">Get Ajax Data</button>
<div id="div-1"></div>

<script>
$("#getData").click(function () {
   $("#div-1").load('targetFile.txt');
});
</script>
</body>
</html>

Source examples: jQuery Ajax Shorthand Methods

This is what i tried with jquery but for some reason its not working.

function getContent(){
$.getJSON('http://oneurl.net/something?callback=?',
function(data)
<script>
$("#getData").click(function () {
$("#div-1").load('targetFile.txt');
});
</script>
}

So you are trying to get JSON formatted data? Your example above is not formatted correctly. you have code outside of the <script> tags and are not using the appropriate jQuery methods for JSON.

Also, does the page you are retrieving data from have the data in JSON format?

Once you retrieve the JSON data, you have to work with the data and do something with it...store the values in an array, display in HTML elements, etc..

There's not enough information here to provide you with a better answer.

Here is a snippet of code that will access a page called json.php. json.php (assuming) will supply data back in json format (ContentType = "application/json";) and will display the results in the browser's console log.

<script>
       $.ajax({
           dataType: "json",
           url: "json.php",
           success: function (data) {
               for (var index = 0; index < data.length; index++) {
                   console.log(data[index]);
               }
           }
       });
</script>

this is coming from an example I was assisting on another thread: Decode json with jquery

Sorry about my english first is not my native language, my question is how to make request in a specific URL. example(https://mywebsite.com/Personal/Data).
I want to use a Json and if the request is successeded i want that data to be stored inside a div tag called Test.

Hello Kleon,

I've enjoyed helping you this far. At this point the help you are asking for is very generic. Up to now, the code examples that have been provided to you are basic generic snippets of code.

What you are asking for now is custom and based upon your vague description, it's to challenging to help you.

I'd recommend that if your initial questions have been answered, consider this thread as being "solved".

Then with regard to Ajax and JSON, start a new thread with by posting very specific questions along with your server side code, sample json data, and client side script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.