javascript - the HTML script tag and non-JS content - Firefox - Stack Overflow

It appears this code will request the file in Chrome and IE but not in Firefox.<script type="te

It appears this code will request the file in Chrome and IE but not in Firefox.

<script type="text/my-custom-mime-type" src="test.ashx">
</script>

Is there a some spec that says browsers should only process JavaScript related mime-types? I know IE probably supports this because of the history with vbscript.

Once you have "content" like this downloaded how can you get access to it? Does JavaScript/jQuery/? have some way of getting at this.

UPDATE So there is 2 parts to question. Sounds like for the first part - the browser will download what it will download and I guess there isn't much you can do about that based off the answers so far.

Example:
<script type="text/xml-script">
<page xmlns=""> <ponents>
<application load="page_load" />
</ponents>
</page>
</script>
</pre>

this is a snippet from Microsoft's declarative MSAjax tech. Could you pull this in from an external file. Note: I'm not trying to use MSAjax here, but its a good example of a custom type being used for a script tag.

Part 2 - can you get access to the text if the "content" does download? For example, lets say its JavaScript - could you display it in a textbox? (without an explicit Ajax call)?

It appears this code will request the file in Chrome and IE but not in Firefox.

<script type="text/my-custom-mime-type" src="test.ashx">
</script>

Is there a some spec that says browsers should only process JavaScript related mime-types? I know IE probably supports this because of the history with vbscript.

Once you have "content" like this downloaded how can you get access to it? Does JavaScript/jQuery/? have some way of getting at this.

UPDATE So there is 2 parts to question. Sounds like for the first part - the browser will download what it will download and I guess there isn't much you can do about that based off the answers so far.

Example:
<script type="text/xml-script">
<page xmlns="http://schemas.microsoft./xml-script/2005"> <ponents>
<application load="page_load" />
</ponents>
</page>
</script>
</pre>

this is a snippet from Microsoft's declarative MSAjax tech. Could you pull this in from an external file. Note: I'm not trying to use MSAjax here, but its a good example of a custom type being used for a script tag.

Part 2 - can you get access to the text if the "content" does download? For example, lets say its JavaScript - could you display it in a textbox? (without an explicit Ajax call)?

Share Improve this question edited Aug 13, 2010 at 15:06 BuddyJoe asked Aug 13, 2010 at 14:38 BuddyJoeBuddyJoe 71.2k115 gold badges301 silver badges473 bronze badges 7
  • What code? Please post a proper example – mplungjan Commented Aug 13, 2010 at 14:41
  • I dont understand the question, can you give an example or elaborate? – David Yell Commented Aug 13, 2010 at 14:41
  • If it is javascript why don't you declare it as such and if its not javascript what do you expect Firefox to do with it? – Chris Commented Aug 13, 2010 at 14:48
  • I expect Firefox to load the content, but then to do nothing with it if its unfamiliar with the mime-type. – BuddyJoe Commented Aug 13, 2010 at 15:14
  • Yeah, I think I'm starting to see what you are trying to do. I'd have thought what you want to do is have your test.ashx return something readable as javascript (eg JSON). Are you ale to change what test.ashx outputs? Can you show us the kind of thing it outputs if it is fixed? – Chris Commented Aug 13, 2010 at 15:23
 |  Show 2 more ments

3 Answers 3

Reset to default 6

Is there a some spec that says browsers should only process JavaScript related mime-types?

See the type attribute:

This attribute gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.

If you want to fetch arbitrary content for use in a script, use XMLHttpRequest.

The canonical way to specify script is

<script src="something.js" type="text/javascript"></script>

or

<script src="somethingThatWilReturnJavaScriptMime.someextension" type="text/javascript"></script>

There is no reason the browser should load unknown mime into a script tag and it will be strictly browser specific whether or not it will allow/ignore the type attribute

It would be a matter of testing to see what the browser will do if you actually send

content-type:text/javascript

regardless of type attribute

Are you setting the content type. Guessing .NET here so posting basic idea:

public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
         context.Response.ContentType = "text/javascript";
         context.Response.Write("alert('hello world');");
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744306200a4567725.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信