الكشف عن قدرات مستعرض

وعلى الرغم من أن برامج استعراض تعتمد معظم ميزات JScript، يتم دعم الميزات الجديدة التي تستهدف.NET Framework، المستندة إلى فئة الكائنات وأنواع بيانات، التعدادات، توجيهات الترجمة الشرطية والبيان ثابتة ، تشغيل جانب الملقم فقط. وبالتالي، فيجب عليك استخدم هذه الميزات بشكل خاص في برامج نصية من جانب الخادم. لمزيد من المعلومات، راجع معلومات إصدار JScript.

يمكن لبرنامج نصي JScript اكتشاف قدرات المحرك الذي يفسر أو تجميع. Th هو هو غير ضرورية في حالة كتابة تعليمات برمجية لتطبيق من جانب الملقم (المراد تشغيلها في ASP أو ASP.NET) أو برنامج سطر أوامر، حيث يمكنك سهولة dهوcover الإصدار JScript المعتمدة وتعليمات برمجية وفقا لذلك. However, when تشغيل العميل-side برامج نصية في a مستعرض, this detection هو important إلى ensure that the برنامج نصي هو متوافق مع the JScript engine في the مستعرض.

There are الثاني ways إلى تحقق من JScript توافق, either بواسطة using the برنامج نصي engine دالات أو بواسطة using conditional compilation. There are advantages إلى using كلاهما approaches.

برنامج نصي Engine دالات

The برنامج نصي engine دالات (ScriptEngine, ScriptEngineBuildVersion, ScriptEngineMajorVersion, ScriptEngineMinorVersion) return معلومات حول the الحالي الإصدار of the برنامج نصي engine. لمزيد من المعلومات، راجع الدالات (JScript).

For the most توافق, فقط features found في JScript الإصدار 1 should be used في a الصفحة that checks supported JScript versions. If an engine supports a الإصدار of JScript higher than 1.0, you can redirect إلى another الصفحة that يتضمن the متقدم features. This means that you must have a separate الإصدار of each ويب الصفحة corresponding إلى each الإصدار of JScript you want إلى دعم. في most situations, the most efficient الحل هو إلى have فقط الثاني الصفحات, واحد designed for a particular الإصدار of JScript, و the غير ذلك designed إلى work without JScript.

ملاحظة

JScript تعليمات برمجية that uses متقدم features must be placed في a separate الصفحة that ليس تشغيل بواسطة المستعرضات مع incompatible engines. This هو mandatory because the برنامج نصي engine of a مستعرض interprets الجميع the JScript تعليمات برمجية contained في a الصفحة. Using an if…else كشف إلى تبديل between a حظر of تعليمات برمجية that uses the latest الإصدار of JScript و a حظر of JScript الإصدار 1 تعليمات برمجية will not work for older engines.

The following مثال illustrates the استخدم of the برنامج نصي engine دالات. Since these دالات were introduced في JScript الإصدار 2.0, you must أول determine if the engine supports the دالات قبل attempting إلى استخدم them. If the engine supports فقط JScript الإصدار 1.0 أو does not recognize JScript, the typeof عامل will return the سلسلة "غير معرّف" for each دالة اسم.

if("undefined" == typeof ScriptEngine) {
   // This code is run if the script engine does not support
   // the script engine functions.
   var version = 1;
} else {
   var version = ScriptEngineMajorVersion();
}
// Display the version of the script engine.
alert("Engine supports JScript version " + version);
// Use the version information to choose a page.
if(version >= 5) {
   // Send engines compatible with JScript 5.0 and better to one page.
   var newPage = "webpageV5.htm";
} else {
   // Send engines that do not interpret JScript 5.0 to another page.
   var newPage = "webpagePre5.htm";
}
location.replace(newPage);

الترجمة الشرطية

يمكن إخفاء المتغيرات الترجمة الشرطية وعبارات تعليمات برمجية JScript من مشغلات لا دعم الترجمة الشرطية. Th هو المنهج هو مفيدة إذا كنت ترغب في تضمين مقدار مربع متوسط من رمز بديل مباشرة في صفحة ويب.

ملاحظة

لا استخدم متعدد الأسطر التعليقات ضمن الترجمة الشرطية كتل حيث قد misinterpret المشغلات التي لا تدعم الترجمة الشرطية إليها.

<script>
/*@cc_on
@if(@_jscript_version >= 5 )
// Can use JScript Version 5 features such as the for...in statement.
// Initialize an object with an object literal.
var obj = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"};
var key;
// Iterate the properties.
for (key in obj) {
   document.write("The "+key+" property has value "+obj[key]+".<BR>");
}
@else 
@*/
alert("Engine cannot interpret JScript Version 5 code.");
//@end
</script>

إذا كان الشرطي @ifحظر يتضمن كثير من تعليمات برمجية، قد يصبح أسهل إلى استخدم الطريقة الموضحة أعلاه لاستخدام مشغل برنامج نصي الدالات.

راجع أيضًا:

المبادئ

معلومات إصدار JScript

موارد أخرى

كتابة وترجمة وتصحيح JScript تعليمات برمجية

الدالات (JScript)

الترجمة الشرطية