简体   繁体   中英

How to abstract a JavaScript file?

I've got a difficult question. I could not find an answer on the internet...so this is my last chance.

I would like to realize the following scenario:

  • One File JavaScript that is "abstract", ie, when the client is mobile, I use mobile Javascript implementation; otherwise I use the classic one.

In other words:

How can I create an A.js file with functions\\Other (For example a HelloWorld() ) and, by delegation or inheritance (or other solution that I ignore)? Can I use the HelloWorld() of A-mobile.js or the HelloWorld() of A-classic.js?

Thanks a lot!

I dont think you want solve this on class level of your javascript. Instead, use different JS file for mobile and full versions. You can do this on server side (render different HTML script tag for each) or on client (javascript can load another javascript like this:

if (mobile){
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", mobileJsFilename)
  document.getElementsByTagName("head")[0].appendChild(fileref)
 }

As for detecting mobile browser, look here : http://detectmobilebrowsers.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM