It is the custom of the Chinese people to celebrate the New Year before the fifteenth day of the first lunar month. Here I would like to wish your friends in the garden a happy new year.
In the past few days, the company's website homepage needs to be revised. After the company's "staff reduction" at the end of last year, one person has to do the work of multiple people, and suddenly he feels that the burden is heavy. No, this is not something within the scope of my work. Unfortunately, I have been involved in it. Fortunately among the misfortunes, the task assigned to me by BOSS this time is exactly the front-end development task I have always been passionate about. I have been engaged in the development of back-end management programs for the company's website before, mostly processing business logic on the server side. I have never had the opportunity to put my skills into use in front-end development, which I am passionate about. Practice is the best way to test true knowledge. Solving the practical tasks assigned to me is a rare test. I learned a lot of scattered knowledge through books and various materials, but did not get the opportunity to combine them together. Comprehensive test”, haha. There are so many ink stains on the front, which are caused by suppressing them for a long time, haha.
Let me first describe the task requirements: There is a JPG picture composed of five balls on the homepage of the company's website. It is used for navigation. Clicking on the text on each ball will open the corresponding information. On the secondary page, there is an almost identical Flash version corresponding to the image. One of the tasks assigned to me by my boss is: when the client browser has a Flash file player installed, the Flash version of the navigation is displayed, and vice versa, the JPG image navigation is displayed. After getting the task, think about it for a moment. As a front-end development, of course you have to consider browser compatibility issues. The best way to bridge the gap between browsers is to use one or more mature JavaScript frameworks. Fortunately, there happens to be one A very mature and sophisticated JS framework exists, named: SWFObject.js.
The first time I came into contact with SWFObject.js was its V1.5, and this time I used V2.1 to solve the problem. There are still some differences in the use of the two. Overall, I feel that V2.1 is a big leap forward compared to V1.5. V2.1 is more in line with the object-oriented JavaScript programming style in terms of the source code of the framework and the usage process.
I will take you to experience this "tortuous" journey from the perspective of a researcher who has just explored JavaScript. Whether you are a novice like me, or you are already proficient in writing various JS As a veteran of coding, please be merciful and hope that everyone can point out the short-sightedness in my thinking and the errors in my writing in a civilized manner.
The following code is an example of usage that I adapted from a documentation of SWFObject V1.5 (if you want to learn more about V1.5, please click this link):
If you want to briefly understand the meaning of each parameter in SWFObject(), please refer to the documentation, which I won’t repeat here.
I strongly recommend that you copy the code in "V1.5 Usage Example" into Notepad, and click SWFObject V1.5 to download the source file of the required V1.5 framework. After decompressing, find swfobject_source.js (uncompressed version, the file name of the compressed version is swfobject.js) file, rename the notepad file to demo.html and place it in the same folder as the swfobject_source.js file. Then please install it in IE6/IE7 and fox respectively. Run it in any browser such as , opera, safari, navigator, chrome, etc. and see what the result is.
If you follow my suggestions, you should find that this picture
is displayed on the page instead of a Flash file. Why is this? If you happen to have the IE series installed on your PC, please follow the steps below: Click the IE browser icon, find the "Tools" menu on the toolbar, select "Internet Options", and click "Advanced" in the window that opens. , find the "Disable Script Debugging (Internet Explorer)" option, uncheck the box in front of it, and click "OK". After completing the above operations, please browse the demo.html page again. Will you find an error prompt box popping up with the following error message: "A runtime error has occurred. Do you need to debug? Line: 117 Error: 'null' is Empty or not an object. ”
If you happen to be using the VS 2003/2005/2008 series of IDEs for development, then I don’t need to teach you how to debug JavaScript code. You can var so = above... Open a debugger, and then debug and trace it. Keep pressing F11 until you trace the inside of the swfobject_source.js file through the so.write() method. You will find that the actual parameter "flashcontent" passed to so.write(elementId) is in the document. The value of .getElementById("flashcontent") is always null. Why is this? Found the problem?
Haha, if you are still a newbie who doesn’t know much about JavaScript, you will be as confused as I was at that time. After many times of debugging and modifying the code, I firmly believe that what I wrote There is no error in the JS code itself. Is there a problem with the externally loaded swfobject_source.js file? If there is a problem, where is the problem? At that time, I was looking for a solution to the error, and I modified the above code into the following example:
If you execute the above code, you will find that this picture is still displayed on the page
, and a warning box containing "The element does not exist" will pop up. It seems that the problem is not caused by external loading. swfobject_source.js file.
If you see this, you will definitely experience my annoyance at that time. After taking a short rest, I cleared my mind and looked back, only to find that the essence of the problem lies in "HTML DOM loading". In a page, JS scripts in the head of the page (that is, between ) and JS files loaded from external files will be executed before the HTML DOM is actually constructed. Therefore, the scripts executed in these two places cannot access the DOM that does not yet exist. You should know the real reason, which is that during the execution of the JS code in Example 1.1, the
...
that has not yet been constructed is accessed.
Okay, after seeing this, there is one last step that you need to do yourself, which is to simply modify the above code and adopt an inelegant method to solve it
About "HTML DOM" "Loading" problem, which method is it? I think you may have guessed it. Yes, it is exactly the following method:
DEMO _fcksavedurl=""swfobject_source.js">"
< body>
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31