1

I'm trying to display the return data from the external Javascript.

Here's my code

global-functions.js

function CurrentDate() {
var url = CurrentDateUrl();
$.get(url, function (e) {
    var Date = e.toString();
    console.log(e);
    console.log(Date);
    return Date;
});
// RETURN the Current Date example. 11/29/2013 10:57:56 AM
}

Sample.cshtml (View)

<h2>Transaction Date: <b><span id="TransactionYear"></span></b></h2>

<script>
function CurrentDateUrl(){
    return '@Url.Action("CurrentDate", "Global")';
}


$(document).ready(function () {
    var Date = CurrentDate();

    document.getElementById("TransactionYear").innerHTML = Date; // the return is UNDEFINED

});
</script>

As we can see in global-functions.js there is no problem as it return from what i wanted but when I try to call the function CurrentDate() it will return to UNDEFINED . Any other way to display it? or other good approach?

EDIT :

Question : Can you verify that function CurrentDate() is called?

  • Yes. As I try to return the hard coded string in CurrentDate() it will display.

I tried the suggested answer below

function CurrentDate() {
var url = CurrentDateUrl();
var result;
$.get(url, function (e) {
    var date= e.toString();
    console.log(e); // will return the Date in console
    console.log(date); // will return the Date in console
    result = date;
    console.log(result); // will return the Date in console
});
console.log(result); // will return UNDEFINED in console
return "Sample";

}

OUTPUT

Transaction Date : Sample

12
  • @Shiven What do you mean by linked? If calling the javascript is what you mean then I already called it. the return data is Undefined and not the function Commented Nov 23, 2016 at 2:22
  • shiven means did you included your global-functions.js in html view, and that too after script tag where you defined function ? Commented Nov 23, 2016 at 2:24
  • 1
    You are also redefining the Javascript keyword Date() with your variable name. I'd choose something else: at least date = rather than Date = Commented Nov 23, 2016 at 2:26
  • @shyammakwana.me Yes, I included the global-functions.js in my shared _Layout.cshtml cause I also used it in my other views Commented Nov 23, 2016 at 2:29
  • Can you verify that function CurrentDate() is called? Commented Nov 23, 2016 at 2:31

1 Answer 1

2

I think you are using $.get() function in wrong way! Please see following link for more info. It cannot return value, it should execute callback function when request is finished!

You should pass function as callback in $.get() (which you are doing) and in that function do logic which you want (which you are NOT doing right now.)

I will rather do like this (probably not good in your case because you are using external file):

$.get(url, function (e) {
    var Date = e.toString();
    console.log(e);
    console.log(Date);

    document.getElementById("TransactionYear").innerHTML = Date
});

Or try this in your case (synchronous call):

 function CurrentDate() {
    var url = CurrentDateUrl();
    var result;

    $.ajax({
    url: url,
    type: 'get',
    async: false,
    success: function(data) {
        result = data;
    } 
 });
 return result;
}

Please note: I am using $.ajax() and I am not returning any value inside $.ajax. Also I added async: false. Now you can return result in that way but it is not async.

If you want to use asynchronous request you have to use callback function. Some implementation can be like in following example:

  function CurrentDate(callbackFunction) {
    var url = CurrentDateUrl();
    var result;

    $.get(url, function (e) {
        var Date = e.toString();
        callbackFunction(Date);
    });
}

// Call your function in code like this
CurrentDate(function(result) {
    // handle your result here
});
Sign up to request clarification or add additional context in comments.

2 Comments

This is not the answer which solves OP's question. He's getting undefined error.
@shyammakwana.me How we can expect value from this line: var Date = CurrentDate(); $.get() doesn't return value?

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.