SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    outputing a json string and exit (like die() in php)

    Hello,

    PHP situation: When a given endpoint of my API is reached, a json string is outputed for being consumed. I sent the proper headers, echo the string and exit the script using die(). I could do it more brutally just by doing die($json_string);

    I was wondering if the same could be done using javascript. I know that throw new Error(json_string); would pretty much do it. But is that good practice? Would window.stop be of any use?

    I expect questions such as: why you wanna do that? why don't you do it in php? answer: for the sake of experimenting and learning.


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    I know that throw new Error(json_string); would pretty much do it. But is that good practice?
    When it's a programmer error that causes the problem, it is good to have the program fail as quickly as possible so yes, in such cases it is a good practice.

    If it's a user error, such situations are where it's not good to throw errors. It's best there to be broad in what functions accept, and narrow in what they return.

    A good place in which to gain some further details on this topic is the Error Handling chapter of the Eloquent JavaScript book and online resource.

    Quote Originally Posted by rhgiant View Post
    Would window.stop be of any use?
    No, that wouldn't be of much use. That does exactly the same as when you press the stop button on your web browser, so it will stop images from continuing to download, or new windows from loading, but not much else.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So there is no way to just output a json string in javascript and not go any further than that point? (again with the die() php function).

    Code:
    if (myvar == 'output') {
    die('i am a string');
    }

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    So there is no way to just output a json string in javascript and not go any further than that point? (again with the die() php function).
    That's what throwing an error does for you. You can issue a stop command after throwing the error, but really it's not appropriate to write JavaScript in a PHP manner.
    Both are quite different languages that each have different environments and situations to worry about.

    When you're writing code in a certain language, it's pays to write it in the language that you're writing in, using the techniques that work for that language.
    English spoken with a French accent can be quite wonderful. With computer programming languages it's not quite as nice.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how would you solve that particular problem? Javascript doesn't seem to be the right tool for doing what I'm trying to do, which I somehow expected...

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    So how would you solve that particular problem? Javascript doesn't seem to be the right tool for doing what I'm trying to do, which I somehow expected...
    Well, what is it that you're wanting to do?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    So how would you solve that particular problem? Javascript doesn't seem to be the right tool for doing what I'm trying to do, which I somehow expected...
    Well, can we get some more details on what it is that you're wanting to do?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the delay, Paul.

    On a certain page of my site, in a certain scenario, I need the script to halt its process and display a message. I'm using PHP's die() function. It's not an error that is thrown. However, I'm working now on a version of that site that is not PHP enabled. I was thinking a javascript solution would be nice to achieve the same purpose.

    I know it must all sound obscure, and I think that I'm going to try to find another way. A more appropriate one to javascript. My main issue was to try to translate PHP into js, which I realize now is wrong.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •