[英]How do you set the document title using Purescript?
After searching for some time I found in Pursuit the module DOM.HTML.History
which has the data type DocumentTitle
.搜索了一段时间后,我在 Pursuit 中找到了具有
DocumentTitle
数据类型的模块DOM.HTML.History
。 This type could probably be used together with the function这种类型可能与函数一起使用
replaceState ::
∀ e. Foreign -> DocumentTitle -> URL -> History -> Eff (history :: HISTORY | e) Unit
To change the document.title
property of the page, however, I can't find examples showing how to call this function (eg, where do I get the external Foreign
data type?).但是,要更改页面的
document.title
属性,我找不到显示如何调用此函数的示例(例如,我从哪里获得外部Foreign
数据类型?)。 Also, I'm not even sure if this function would do what I expect it to do...另外,我什至不确定这个功能是否会做我期望它做的事情......
In the unfortunate case that the Purescript team didn't include in their core API a way to change the document title, it's still possible to do so by making use of purescript's handy FFI mechanism.不幸的是,Purescript 团队没有在他们的核心 API 中包含更改文档标题的方法,但仍然可以通过使用 purescript 方便的 FFI 机制来做到这一点。
Add these two files into your project:将这两个文件添加到您的项目中:
Document.js文档.js
exports.setDocumentTitle =
function (title)
{
return function ()
{
window.document.title = title;
};
};
Document.purs文件.purs
module Document
where
import Control.Monad.Eff (kind Effect, Eff)
import Data.Unit (Unit)
foreign import data DOCUMENT :: Effect
foreign import setDocumentTitle ::
∀ fx . String -> Eff (document :: DOCUMENT | fx) Unit
Now you can call setDocumentTitle
as you would call Console's log
function, except the effect would be DOCUMENT
instead of CONSOLE
, of course.现在您可以像调用 Console 的
log
函数一样调用setDocumentTitle
,当然,效果是DOCUMENT
而不是CONSOLE
。
kazouas answer would look like this (in PS 0.12) kazouas 的答案看起来像这样(在 PS 0.12 中)
import Effect (Effect)
import Data.Unit (Unit)
foreign import setDocumentTitle :: String -> Effect Unit
Javascript remains the same. Javascript 保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.