i have this menu in javaScript:
menu[1] = {
id: 'menu1', //use unique quoted id (quoted) REQUIRED!!
fontsize: '100%', // express as percentage with the % sign
linkheight: 22, // linked horizontal cells height
hdingwidth: 210, // heading - non linked horizontal cells width
// Finished configuration. Use default values for all other settings for this particular menu (menu[1]) ///
kviewtype: 'fixed',
menuItems: [ // REQUIRED!!
//[name, link, target, colspan, endrow?] - leave 'link' and 'target' blank to make a header
[prompt("add a note:", ""), "", ""]
]
};
i want to replace the prom开发者_JS百科pt with data from a database...
As your page is being generated by whatever server script you are using, get the data from the database and wriote it as part of your javascript.
Whatever you do, DO NOT ATTEMPT TO CONNECT TO THE DATABASE DIRECTLY FROM JAVASCRIPT. Doing this will allow any user to manipulate the javascript and run any queries they want on your data.
http://bytes.com/topic/javascript/answers/532398-connect-access-database-using-javascript
As CK has said this is one of them things you can do, but shouldnt. However since you asked, the code is in the post above.
I'll try to detail what ck suggested. It's not trivial, I think you need:
- server-side, at system level, you need to setup a sql engine, for example MySQL.
- server-side, at application level, an abstraction layer which checks data requests and translates them into sql queries.
- client-side, an AJAX layer to request for data pages on the fly.
This means you have to build up quite some stuff, just for a menu.
Alternatively, you may machine-generate the JavaScript code which initializes your data structure, via a script in whichever language you choose. Pseudo code:
print "menu[1] = {"
print "id: 'menu1'"
//etc. for constant lines
//...
print "menuItems: ["
result_set = <local sql query>
for (row in result_set)
print "[ "
print row.name
print ", "
print row.link
print ", "
print row.target
print "]"
next
This is not as dynamic: it cannot be updated per request, you have to re-run it and substitute the generate Javascript on the server, but if orders of magnitude easier to implement (it also depends on your know-how and privileges on the server, of course).
1 Create a ASP.NET server page that spits out XML with all the menu notes.
eg
<data>
<note id=1>My note 1</note>
<note id=2>etc...</note>
</data>
2 Modify the .NET page code to get the note count and note text from SQL and incorporate it in the XML stream (keeping the connection string on the server).
3 Write some Javascript onLoad= code if necessary to setup the menu without the notes to begin with (as the rest of the data will come in asynchronously slightly or a lot later or not at all depending on network ie certainly not immediately at onLoad= time).
4 Write some async Javascript code that pulls down the XML and uses the DOM model of the XML to count the nodes, pull out the note text, push it into the menu object in the right places and update the visible counter-parts to the menu at that point if apt.
5 Write some fallback code for if AJAX doesn't work on the particular user's browser platform eg server-generated Javascript as Andrea shows above, or just non-display of menu notes if apt.
I get the feeling from how you say this that the requirements will change soon so that you'll want more schema fields and to give more prominence in the UI to the dynamic aspect of this process than you are hinting. But this should get you off to a good start in getting something running and also started going off in the right direction technically.
精彩评论