Results 1 to 8 of 8

Thread: Intercept event on an image JS has put itself

  1. #1
    Join Date
    Mar 2012

    Intercept event on an image JS has put itself


    Then, my kingdom game with rats in JS is on the road. I even started to make it have "turns". Work well (thanks again for the innerHTML solution, guys).

    Actually, to wait between turns, i'm using an "alert box". But i want something else.

    What i'm wanting is :

    - The script put an image "next turn" on the page, with a createElement.
    => work well, the image appear.

    Then i put a loop for the turns.
    => work well too. You can choose starting turn, ending turn, increment, and if there is a turn "0" (when starting is negative and ending positive, by example. You know year 0 didn't exist, of course).

    My problem is now :

    How do my script can intercept an event (a click will do) on a image it put itself in the page ?

    Of course, i searched for explanations and tutorials, but those i found speak of an "onclick" on the image calling a new JS script, and i don't know if trading data between scripts is even possible.

    An idea ?
    Last edited by Ddurand; 04-09-2012 at 06:30 AM. Reason: spelling

  2. #2
    Join Date
    Feb 2012
    Pensacola, FL
    Something like this:

    document.getElementById('someId').innerHTML = '<img src="SomeImage.png" onlick="doSomeJS()" />";

  3. #3
    Join Date
    Mar 2012
    I understand, but what i want is a way to make the same script intercept the "onclick" (onlick ? You wish !). With your example, a click call another script.

    I want, basically, that :

    JS script put the image on screen.

    JS script enter an infinite loop (a while waiting for a condition==true, by example).

    When the image is clicked, the condition of the loop is put on "true", then the script exit the loop.

    Sorry if it wasn't clear enough. English is not my primary language.

  4. #4
    Join Date
    Feb 2012
    Pensacola, FL
    I am 99&#37; sure this cannot be done because until the function has ran, it pretty much ignores all input. What is your goal, maybe I could help think of an alternative.

  5. #5
    Join Date
    Mar 2012
    File : The game. Beware, the script is NOT optimized (no cache for the pictures). On my Linux computer, it's too much for Opera, but good for Firefox.

    Basically, it's a kingdom game (like "Civilization" and so). I want him to be only client-side : The player download it, put him in his computer, then open the html file (pictures are in subdirectories).

    The script begin by drawing the map, putting ground, trees, cities. Each element has is own ID.

    After it has finished, he run a loop for the turns, and for now put the actual turn number on screen (no problem with that).

    I tested it by putting an alert box in the loop : Work well, the game stop at each turn until a click on the box button.

    But an alert box is ugly, and the player can wanting to do a lot of others things than clicking on an alert box button, like clicking on a city or an unit, opening a menu, and so on.

    Then i though i could make the script waiting for events like that :
    just before the end of the first loop, i put another loop (i think of a while, that wait for an event, like clicking on the "next turn" button. If the player click on the "next turn" button, the condition is met and the script exit the waiting loop, and go for his next iteration of the first loop.

    I though, then, to put in the waiting lood some coding for capturing others events, like clicks on a city then call this function, click on an unit then call that function, etc.

    Well, like that :

    FOR loop (that's the loop the the games turn. That work well).
    Show turn number. Shop total population. Waiting loop (exit on a event "click on the element with the ID X").
    if event "click on City X" ix true, then go to function "show city panel".
    end Waiting loop.
    end FOR.

    Oh got, i though my Internet connection was cut, but it was just the first box crash in months. My heart...

  6. #6
    Join Date
    Mar 2012
    After hard brainstorming, i finally discovered how to do... to be kicked in the balls by the fact that when a javascript run too long (and too long mean very short), the browser complain and ask for stop it.

    I think i'll look now for a mean to put datas directly on the page, maybe by using the elements' array. I will see.

    My idea (work, until the browser complain) was that :

    When the JS create the image, add a :


    Then in the turn loop, i add a waiting loop :

    var i=1;
    while (i==1)

    then the little function here do the job :

    function tour()

    Mean :

    When JS put the "next turn" image on the screen, it add it an onclick calling a new "tour" function.
    Then it add, on the same image, an attribute called "name" with the value "1".

    When the player click on the image, the function "tour" is called, and change the image attribute "name" from 1 to 2.

    BUT, in the waiting loop, a "getAttribute" read periodically for the value of "name", and put it in the "i" variable.
    And what happen when i == 2 ? The while of the waiting loop is FALSE, then loop exit for the next turn of the game.

    That was so cool. I'm pretty sad actually, because i that was a big work for me, and damnit that worked !

    That mean creating an entire game with ONE JS is probably impossible, because browsers don't accept "long" pauses.
    As i said, i will look now in storing datas directly in the page, probably in images attributes, or maybe in an hidden layer, i don't know for now.

    See ya later, i need to drown myself (in movies), now.
    Last edited by Ddurand; 04-10-2012 at 04:59 PM.

  7. #7
    Join Date
    Mar 2012
    You cannot infinitely loop JS. You can, however, use setInterval and setTimeout pointing to a seperate function to make the browser "wait".

  8. #8
    Join Date
    Mar 2012
    Thanks Gray1989, but i found another solution :

    The main (onload) JS just put the map and games elements. He create, by example, the "next turn" button and "setattribute" with elements like "last turn", "present turn", "first turn", etc.
    Then this script end and the player has the hand.

    Then by clicking on the "next turn" button, you call another script that read the value of the "present turn" of the "next button", add the increment (do some unspeakables others things), then display the turn number (by an innerHTML), et put this number in the "present turn" attribute.

    I hope i'm clear. I think separating activities (turns calculations, city panel, unit moves, battles, etc.). is more do-able if i can find quickly the data when i need them.

    For now, i read a page on #menu and queryselector. Hope that help me.
    Last edited by Ddurand; 04-10-2012 at 07:11 PM. Reason: Spelling

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10695 seconds
  • Memory Usage 2,922KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates