Replacing part of webpage with javascript - good? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is it a good programing practice to replace a layout component with another without a refresh?
e.g. say i want to sort items. Is it a good idea to sort them using javascript without any refresh, or should i make a refresh and call php to sort the data?
EDIT: I understand the way asked, this question is too open. We got two ways to do things.
Server side:
a) depends on server and users with 128MB ram pc will have no issues.
b) requires resources (bandwidth and cpu power) on the server side, while makes the user wait for the page to load.
client side:
a) done like a magic. if the user got an i7 cpu it will work like magic. Also no resurces wasted and no wait time.
b) if the user got a slow pc, will it take time for the action to be performed? Say, I want to use javascript to replace 50 small images (icons) and 300 textfields. Should i do this with javascript or php? If the user got a 2000 pc, will he notice a delay or something?

Almost every AJAX call you make is going to manipulate the DOM with Javascript. I think it largely depends on what you are trying to accomplish, but I don't think there is a reason to force the user to do an entire round trip to the web server if there isn't a good reason to.

Well, first off, you can answer this question with whether or not you want the sorting done server-side or client-side. If you want client-side then stick to the JavaScript, otherwise go Server-Side. Depending on how big the sort is/ hardware support/ etc. you may want to keep it client-side.
Now, if you use php, or some other language, and refresh the page, then you will have to require the user to redownload the page to see the new content. If this doesn't matter then do it that way.

Related

Can i generate a hash of a javascript file to check if it has been edited by the client? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am using https://pavlovia.org to run some experiments in behavioral economics in risky-choice. The task requires the subjects to play (or not) different lotteries. The system creates javascript files which it runs on the subject's browser. However, I found it very easy to inspect and edit the javascript which would allow a malicious subject to give themselves an advantage (e.g. increase the probability of a lottery paying out).
If someone makes all the lotteries "win", it will be easy to tell they cheated, but if they boost the probabilities by a small amount it will be very hard.
Other than re-writing everything so that the logic is server-side, I wondered whether it is possible to compute a hash (md5 or sha) of the JS code on the client and sent it back to the server to check wither the JS has been edited. I found several posts related to computing hashes in javascript, but I wasn't sure whether a file could compute its own hash.
I appreciate that a sophisticated user could also hack that part of the code (e.g. copy the original file to another name and then edit the code that takes the hash to point to the original file, or something like that). But i wonder if this technique could be one more barrier to cheating.
The user has the ability to debug the JS code while running and replace values in some variables. Even if JS hashing was possible, it would not prevent that. I believe there is no other way than making part of the crucial logic on the server side.

Which is more preferable in terms of performance Javascript and jQuery or PHP given same type of work [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Description :
I have couple of functions that can be written in JavaScript and PHP aswell.
e.g
(a) Current datetime calculation and their subtraction
(b) String replace , extracting substring , checking for a position of character in string and many more
I can submit the data in two ways either I can perform all the calculations and functions on client side using JavaScript
OR
I can send all the raw data to php and do the exact same work in PHP there before saving it in the database.
My Question :
What should I focus on more and try to write as much as I can in either of the languages ?
If performance is what you're after, do as much as you can in javascript, because client's computer tends to have more free computing power than your server. That is because a client only has to do it for himself, server would need to do it for all visitors - possibly thousands upon thousands. Expected client base is a major factor here.
However, take into consideration that a client can't be "trusted"; if you're going to insert something in database, verify everything that needs to be right.
If supporting visitors without javascript is something you'd care to do, you obviously have to move everything server side, but that is rarely the case nowadays.
If anything that you are trying to do is for validation, then do it on the server side with PHP. The client side is untrustworthy and unreliable for that sort of thing.
As far as efficiency goes, in terms of calculations/processing power, your server is likely going to be beefier than the client. You may have a client with a beast of a machine, but gnerally speaking your server's hardware out perform the client hardware. Better to keep as much of your processing you can on the server.

Do I really need to use ajax for non-page-loading website? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
getting straight to the point, I am designing an online Test Series (/objective exams) website with PHP. The thing I want to tell you here that, I want to load whole test series at once, so that after starting the Test, user don't have to wait up for the next or previous questions. (I am trying to do that with ajax). I am also maintaining the 'question map' for tracking and letting the user know about the attempted questions as in attached demo GUI picture.
Now I have 2 questions..
1) Is it really possible? To maintain question map if the system is loading next pages without calling to the database (As ajax do)
2) How. And do I really need to use ajax template for my purpose.
NOTE: All I want is to load newer questions with no/minimum page load but with maintaining proper DB calls. Any suggestions are appreciated. Or any new way to attain this??
Firstly, you seem a little confused about AJAX. AJAX is the primary method for client side calls. As opposed to using a server side language, like PHP or C# w/ ASP.NET, you can use Javascript to make database calls after the page loads.
What you seem to be asking is if you can load a large amount of data on page load and then organize it with Javascript. This is entirely possible. However, it requires large database calls, as well as more memory(RAM) to store the data, and increased time to load the data. The RAM factor should be relatively small, and ajax would only provide micro-optimizations to that, as long as your data is relatively efficiently stored and not incredibly massive. You should note that database calls can quickly get expensive. If you choose to load all data on page load, will the users consume all of that data? If not, then you are wasting their computer's memory, the time they take to load the page, their bandwidth, as well as making unnecessarily large calls to your own database.
So your main concerns, IMO, should be page load time and reducing the number of unnecessary calls to the database. Will the amount of data being sent negatively impact load times? Are you sending hundreds of records? Will the users consume all of the data if you send it all at once (many websites have 30-50% bounce rates, meaning 30-50% of users leave immediately)? Would it be better to chunk the data and not send half of it until they're ready for the second half?
It sounds like you're only calling the data for a small quiz, which is relatively insignificant. E.g. if your loading 20 questions, and 4 answers, you should just use server side calls. On the other hand, if it's hundreds of questions, with lots of extraneous data, then perhaps you should use AJAX.

Is Header or Javascript better for redirecting in PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am trying to redirect users to a specific page after a login. Are both equally effective? or is one better than the other?
Thanks
You cannot compare both methods!
JavaScript will only run on the client side, therefore you need to send HTML first, then execute the redirection.
Using PHP, you can directly forward the user. This is not only faster, but also the only applicable choice in terms of usablity.
You will also have a problem if JavaScript is disabled or you have got an error in your JS scripts (for whatever reason). Well, you could counter by using HTML meta redirects, but the pros of using server-side redirects just predominate.
Compared to client side JS, an HTTP redirect will:
Require less data to be send to the client for it to process the redirect instruction
Not cause the browser to behave badly when the back button is pressed
Work in browsers with JS blocked / disabled / otherwise failing
An HTTP Location header is almost always the better approach.
They are different.
One is server side (php) and the other is client side (js).
Usually it's done server side, so it's a transparent redirection (doesn't pollute browser history, back button, etc). I repeat: usually but it's not a rule.
PHP is faster - the whole page doesn't have to load first. You can just send the header and exit the script.

Real time collaborative use interface on the web [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm wondering what the technologies and best practices are behind real time collaboration in web interfaces.
An example of this is of course Google Wave. Another is PivotalTracker.com.
I'm particularly interested in any work (frameworks, plugins, etc) people are doing with Ruby on Rails here.
I imagine it would have to use Javascript underneath at some level, but you would need a way to abstract this out. Probably polling the server periodically to see if changes have been made, and also a way to resolve conflicts if in the middle of editing something the server comes back and says someone else has updated it.
Thank you!
Wave has operational transform that has a nice property of being easily combinable. You have two users, each of them does "something" in the user interface and two "somethings" can be combined into final document. That allows you to skip the problems with conflict resolution.
A nice way to enable real-time updates to state of the app is by using Comet, which is essentially a geeky codename for keeping an alive, long standing, unterminated get/post request to the server, that server finishes and responds to when something happens on the server. It allows sending to the client instantaneous updates without having the client periodically poll.
I can't really say how to abstract this away in javascript/r'n'r, many of the underlying technical details are hard enough and application specific that no framework supports them out of the box.

Categories