0

I tried to get value of variable from function beyond it, but always get error: Uncaught ReferenceError: devuuid_raw is not defined

The task is: I need to get value of variable beyond function in another variable because that value need to be used further (send to the server).

Code:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    console.log("Device is ready");
    var element = document.getElementById('devProps');
    devuuid_raw = device.uuid;
    return devuuid_raw;
    element.innerHTML = 'Идентификатор устройства: ' + device.uuid + '<br />';                                               
}

var devuuid = devuuid_raw;

How can I get device.uuid value outside the function?

1 Answer 1

2

First create a variable

var devuuid;

then add the event listener

document.addEventListener("deviceready", onDeviceReady, false);

and simply change the variable inside the function:

function onDeviceReady() {
  console.log("Device is ready");
  var element = document.getElementById('devProps');
  devuuid_raw = device.uuid;
  devuuid = devuuid_raw;
  element.innerHTML = 'Идентификатор устройства: ' + device.uuid + '<br />';
}

If you don't use the 'var' keyword inside the function, it will reference to the variable defined before.

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

9 Comments

Thanks, your solution removed Uncaught ReferenceError, but I still cannot get value from variable, I tried this: var devuuid; document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log("Device is ready"); devuuid = device.uuid; document.getElementById('devProps').value = devuuid; } But variable still not show value :(
@user3166813 how and when do you check devuuid value?
<p id="devProps">Load info...</p> So, js code above should get element by id and show value, but it dont do it, then I send this value with using jquery.ajax, but it also dont work because it cannot get value
try console.log(devuuid) as last line inside the function. does it show the expected value? if you want to access the variable inside the javascript console, you have to create it globally without the 'var' keyword, e.g. devuuid = 0;
it don`t show the value, but I think it should not because device.uuid is specific phonegap feature and works only on phones
|

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.