1

I have this javascript function which allows me to create my custom alerts by calling it using this <script>Alert.render('message goes here');</script>

Javascript:

<script>
function CustomAlert(){
    this.render = function(dialog){
      var winW = window.innerWidth;
      var winH = window.innerHeight;
      var dialogoverlay = document.getElementById('dialogoverlay');
      var dialogbox = document.getElementById('dialogbox');
      dialogoverlay.style.display = "block";
      dialogoverlay.style.height = winH+"px";
      dialogbox.style.left = (winW/2) - (550 * .5)+"px";
      dialogbox.style.top = "200px";
      dialogbox.style.display = "block";
      document.getElementById('dialogboxhead').innerHTML = "Image Upload Error!";
      document.getElementById('dialogboxbody').innerHTML = dialog;
      document.getElementById('dialogboxfoot').innerHTML = '<button id = "buttonclose" onclick="Alert.ok()">ok</button>';

    }
    this.ok = function(){
      document.getElementById('dialogbox').style.display = "none";
      document.getElementById('dialogoverlay').style.display = "none";
    }
}
var Alert = new CustomAlert();

HTML:

<div id = "dialogoverlay"></div>
<div id = "dialogbox">
  <div>
    <div id = "dialogboxhead"></div>
    <div id = "dialogboxbody"></div>
    <div id = "dialogboxfoot"></div>
  </div>
</div>

Inside the html, I tried calling in the function(Alert.render...) and worked. However, when I tried calling it in the php above the html :

echo "<script>Alert.render('message goes here');</script>";

It doesn't work at all. Any ideas?

UPDATE

Here's the php code:

<?php

if(isset($_POST['btn_sample'])){
  echo "<script>Alert.render('sample message');</script>";
}

I added this on the html:

<form action = "" method = "post">
  <input type = "submit" name = "btn_sample" value = "sample">
</form>
3
  • 1
    can you show your php page Commented May 11, 2017 at 8:02
  • you placed the same pieces of code at the same place in your page ? As long as the PHP will execute first and render a HTML page, there sould be no difference. Commented May 11, 2017 at 8:03
  • @Exprator I added the php sir Commented May 11, 2017 at 8:12

2 Answers 2

1

Check this ,

  1. I think you are execute the php script before document load .
  2. Or php included html not containing the link of the this js script file.

And call the Js function after window.onload like this.They will prevent the first one

echo "<script>window.onload=function(){Alert.render('message goes here');}</script>";
Sign up to request clarification or add additional context in comments.

Comments

0

Your render function relies on the elements "dialogoverlay", "Dialogbox", etc. already existing, which they don't if you call the function above your HTML.

You need to either bind your Alert.render() call to the DOMContentLoaded event or just put your scripts at the bottom of your body, which tends to be the preferred practice nowadays.

1 Comment

Are there any errors in your browser console? You could try putting console.log() statement along with your Alert.render() to see if that script gets executed at all

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.