Jump to content

Refresh if the user hasn't clicked on an input field


Recommended Posts

I'm making this little chat room thing and I used

 

echo("<meta http-equiv=\"refresh\" content=\"7;url=http://www.mysite.com?body=chat\">");

 

to refresh every 7 seconds but sometimes you're trying to type when it refreshes...

 

Is there a way to check if the user has selected the field (<input type='text' name='chat' class=generic_black'>) before refreshing?

Link to comment
Share on other sites

Yes, it could be done. My javsacript is a touch rusty, but i think it would require a bit of work.

 

As far as i'm aware, there's no simple way of checking if an element is in focus. Instead, you would have to set a variable to true using the onFocus event and then set it to false with onBlur.

 

You would then need to use the settimeout() function every 7 seconds to call another function which would check the above value and reload only when required.

 

My recommendation: switch to loading the chat content with AJAX. It'd be less resource intensive, and much nicer for the user. If you google for AJAX chat rooms im sure you'll find some examples to follow.

Link to comment
Share on other sites

It think you have to make the page refresh a JavaScript function, that disables count if the user clicks on a field.

 

For example:

 

<input type="text" name="field" value="value" OnClick="stopTime('true');"

 

And the stop time function should have a counter that automatically runs UNLESS a it is called again with a set BOOLEAN

 

My JS is VERY VERY rusty so I won't write the function.

 

Hope it helps

 

Modification:

Using AJAX is probably better because it allows you to update specific parts of your webpage without refreshing,

But I only just started learning AJAX, so I really can't help further on that, sorry

Link to comment
Share on other sites

the reason i said frames is because they are easy.. ajax would be better but its a larger learning curve..

 

with frames your have 3 files

 

main.html <-- main container loads next two

controls.php <-- controls to add text

chatview.php <-- this you have your metatag

 

 

<frameset rows="100px,*">
      <frame name="control" src="controls.php">
      <frame name="chat" src="chatview.php">
</frameset>

 

*untested

Link to comment
Share on other sites

So you're saying I should just refresh a frame as opposed to the whole page?

 

It's a possibility, but I strongly suggest Learning AJAX to pull this off, refreshing frames is more annoying for the user browsing your page.

With ajax you can update information without refreshing you entire page using XML objects.

 

Granted it's not that easy to learn, but if you search there are some good video tutorials.

Link to comment
Share on other sites

Hmm... I think I'll just use frames for now, I don't really understand how to show database entries with AJAX.

 

But with frames, apparently they cant be used inside body tags, but my site basically needs them so can you go:

 

<body>stuff</body>

<frame>chat</frame>

<body>stuff</body>

 

Does that work?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.