简体   繁体   中英

Invalid left-hand side in assignment Javascript

This is my code for opening a popup and doing a HTTP request:
The HTTP request doesn't work yet.
This is my code:

window.onload = addElement;

function addElement() {
// create a new div element 
// and give it popup content 
var newDiv = document.createElement("div");
var texts;

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = 
function()
{

    if (xmlhttp.readyState == 4 && xmlhttp.status = 200)
    {
        texts = xmlhttp.responseText;
    }
    else
    {
        texts = 'Waiting for response...';
    }
}
xmlhttp.open("GET", "localhost", true);
xmlhttp.send();

newDiv.innerHTML += '<div id="popup" style=" position: fixed;top: 15%;width: 800px;height: 200px;margin: auto;z-index: 99999;display: block;left:25%;background-color: #fff;  border: 1px solid #ddd;  border-radius: 5px;  box-shadow: 0 1px 6px 4px #000;  overflow: hidden;   padding: 10px;"><div class="popup_body" style="  height: 160px;">' + texts + '</div><button style="padding: 10px;" class="close_button"onClick="closePopup()">Sluiten</button><button  style="padding: 10px;" class="close_button"onClick="tostoring()">Meer Informatie</button></div>';

// Add The Background cover
var BG = document.createElement("div");
//BG.style.background-color = 'black';
BG.style.width = '100%';
BG.style.height = '100%';
BG.style.background = 'black';
BG.style.position = 'fixed';
BG.style.top = '0';
BG.style.left = '0';
BG.style.opacity = '0.7';
BG.style.zIndex = '99900';
BG.style.display = 'none';
BG.setAttribute("id", "bgcover");

// add the newly created elements and its content into the DOM 
document.body.appendChild(BG);
document.body.insertBefore(newDiv, BG);
// open popup onload

openPopup();
}

function openPopup() {
  var el = document.getElementById('popup');
  var BG = document.getElementById('bgcover');
  el.style.display = 'block';
  BG.style.display = 'block';
}

function tostoring() {
     window.location.href='http://localhost/Sms%20management%20systeem/testing/storing.php';
 }

function closePopup() {
  var el = document.getElementById('popup');
  var BG = document.getElementById('bgcover');
  el.style.display = 'none';
  BG.style.display = 'none';
}

What does this error mean?
How can I fix this error?
I'm stuck because my code doesn't work when I have this error.
I know it could be the if statement but I don't know what is wrong.

xmlhttp.status = 200 should be xmlhttp.status === 200
I made an mistake with the compare of the two values.
Thanks @Francois wahl.

This error is caused by operator precedence:

&& has higher precedence than = so:

 (xmlhttp.readyState == 4 && xmlhttp.status = 200) 

is treated as:

( (xmlhttp.readyState == 4 && xmlhttp.status) = 200)

The left hand side isn't a variable or a property, so you can't assign to it.

You don't really want to assign to it though. You are trying to compare and you made a typo.

xmlhttp.status = 200 should be xmlhttp.status == 200

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