简体   繁体   中英

What is wrong with this code Javascript and Parse

I am using javascript and the parse API and i have this code

<script type="text/javascript">
Parse.initialize("APPID", "JAVASCRIPTCODE");
var currentUser = Parse.User.current().get("FirstName");
document.getElementById("fname").value = currentUser;
</script>

<input type=text id="fname" />

however the code is not adding the data from javascript to the text box where am i going wrong.

The problem is that you're accessing the element before it is added into DOM .

You can wrap the last statement in DOMContentLoaded

The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page).

document.addEventListener("DOMContentLoaded", function(event) {
    document.getElementById("fname").value = fname;
});

Or, you can also move your <script> at the end of <body> tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM