Load a javascript file and css file depending on user agent

后端 未结 2 1974
一向
一向 2020-12-11 05:36

Just like in the title.

I got two files: one is javascript file and one is css file. And if user-agent is an iPad I want to load those files - but o

2条回答
  •  时光说笑
    2020-12-11 06:13

    You can use document.createElement to create link and script elements, and then append them to the document (for instance, append them to document.getElementsByTagName('head')[0] or similar).

    This answer here on SO suggests that you can dtect an iPad by just looking for the string "ipad" in the navigator.userAgent field. Of course, the user agent field can be spoofed.

    So for example:

    
    

    ...but that's off-the-cuff, untested.

    (Note that there's no reason to put the type on either link or script; in the case of link, the type comes from the content-type of the response. In the case of script, the default is JavaScript.)

提交回复
热议问题