run Javascript if failed=1 e.g if ($_GET['failed'] == '1') {employmentAdd();}

Member_2_5230414
Member_2_5230414 used Ask the Experts™
on
javascript:employmentAdd();

How can i run the java script if the failed=1 within the url?

if ($_GET['failed'] == '1') {employmentAdd();}

Open in new window


i am unable to add the into the body load so is there another way around this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<body onload="<?php echo ( isset($_GET['failed']) && trim($_GET['failed']) == 1 ) ? 'employmentAdd();' : '';?>">

Author

Commented:
is there away to do this without using body onload?

Commented:
Yes. Anywhere inside a <script> tag will also work:

e.g.
<body>
  ... your content here ...

  <script type="text/javascript">
  <?php echo ( isset($_GET["failed"]) && intval($_GET["failed"]) == 1 ) ? "employmentAdd();" : ''; ?>
  </script>
</body>

Commented:
Just be forewarned that if you don't add it to the body onload or take any measures to wait until the page has finished completely load (e.g. using jQuery or some other framework to run it after the page loads), then some of the different HTML elements on the page may not be available to the code inside employmentAdd(). But if employmentAdd() doesn't need to access elements on the page, then it may be okay. Make sure you test on all browsers!

Author

Commented:
i have added it into the body but does not seem to run the script

function employmentAdd() {
      document.getElementById('employment_add').style.display = "block";
}
Commented:
Do you have jquery on the page? If so, try:

<body>
  ... your content here ...

  <script type="text/javascript">
    $(document).ready(function() {
  <?php echo ( isset($_GET["failed"]) && intval($_GET["failed"]) == 1 ) ? "employmentAdd();" : ''; ?>
    });
  </script>
</body>

Open in new window


If you don't have it enabled, you can add it by adding this line above the other <script type...> tag:
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
Can you post your URL?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial